从包含对象的数组中拆分数值到不同的对象。

huangapple go评论68阅读模式
英文:

Split values from array with objects to a different object

问题

基本上我有这个数据

```javascript
const arrayWithDates = [{2023-06-02: 2, 2023-06-21: 6}, {2023-06-29: 2, 2023-06-23: 1}]

但我想将前面的数组转换为这个:

const arrayTransformed = [
  [{count: '2', date: '2023-06-02'}, {count: '6', date: '2023-06-21'}],
  [{count: '2', date: '2023-06-29'}, {count: '1', date: '2023-06-23'}]
]

<details>
<summary>英文:</summary>

Basically I have this data:


const arrayWithDates = [{2023-06-02: 2, 2023-06-21: 6},{
{2023-06-29: 2, 2023-06-23: 1}]


But I would like to transform the previous array to this:


Const arrayTransformed = [[{count:'2', date:'2023-06-02'}, {count:'6', date:'2023-06-21'}],[{count:'2', date:'2023-06-29'},{count:'1', date:'023-06-23'}]]


</details>


# 答案1
**得分**: 0

假设您的`arrayWithDates`中的日期是以字符串格式化的,那么以下代码应该可以工作:

```javascript
const arrayTransformed = arrayWithDates.map(obj => {
   return Object.entries(obj).map(([date, count]) => {
   return { date, count: count.toString() };
   });
 });
英文:

Assuming your dates in arrayWithDates are formatted as strings, this should work:

const arrayTransformed = arrayWithDates.map(obj =&gt; {
   return Object.entries(obj).map(([date, count]) =&gt; {
   return { date, count: count.toString() };
   });
 });

答案2

得分: 0

你可以通过将对象转换为条目来将每个记录映射到一个数组。

你需要将每个记录的键映射到一个 date 字段,将相关值映射到一个 count 字段。

如果你想要展平数据,请调用 flat()

输出:

[
  [
    { "count": "2", "date": "2023-06-02" },
    { "count": "6", "date": "2023-06-21" }
  ],
  [
    { "count": "2", "date": "2023-06-29" },
    { "count": "1", "date": "2023-06-23" }
  ]
]
[
  { "count": "2", "date": "2023-06-02" },
  { "count": "6", "date": "2023-06-21" },
  { "count": "2", "date": "2023-06-29" },
  { "count": "1", "date": "2023-06-23" }
]
英文:

You can map each record to an an array by converting the object to entries.

You will need to map each record's key to a date field and the associated value to a count field.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const arrayWithDates = [
  { &#39;2023-06-02&#39;: 2, &#39;2023-06-21&#39;: 6 },
  { &#39;2023-06-29&#39;: 2, &#39;2023-06-23&#39;: 1 }
]

const arrayTransformed = arrayWithDates.map(record =&gt;
  Object.entries(record).map(([date, count]) =&gt;
    ({ count: `${count}`, date })));

console.log(arrayTransformed);

<!-- language: lang-css -->

.as-console-wrapper { top: 0; max-height: 100% !important; }

<!-- end snippet -->

Output

[
  [
    { &quot;count&quot;: &quot;2&quot;, &quot;date&quot;: &quot;2023-06-02&quot; },
    { &quot;count&quot;: &quot;6&quot;, &quot;date&quot;: &quot;2023-06-21&quot; }
  ],
  [
    { &quot;count&quot;: &quot;2&quot;, &quot;date&quot;: &quot;2023-06-29&quot; },
    { &quot;count&quot;: &quot;1&quot;, &quot;date&quot;: &quot;2023-06-23&quot; }
  ]
]

If you want to flatten the data, call flat().

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const arrayWithDates = [
  { &#39;2023-06-02&#39;: 2, &#39;2023-06-21&#39;: 6 },
  { &#39;2023-06-29&#39;: 2, &#39;2023-06-23&#39;: 1 }
]

const arrayTransformed = arrayWithDates.map(record =&gt;
  Object.entries(record).map(([date, count]) =&gt;
    ({ count: `${count}`, date }))).flat();

console.log(arrayTransformed);

<!-- language: lang-css -->

.as-console-wrapper { top: 0; max-height: 100% !important; }

<!-- end snippet -->

Output

[
  { &quot;count&quot;: &quot;2&quot;, &quot;date&quot;: &quot;2023-06-02&quot; },
  { &quot;count&quot;: &quot;6&quot;, &quot;date&quot;: &quot;2023-06-21&quot; }
  { &quot;count&quot;: &quot;2&quot;, &quot;date&quot;: &quot;2023-06-29&quot; },
  { &quot;count&quot;: &quot;1&quot;, &quot;date&quot;: &quot;2023-06-23&quot; }
]

huangapple
  • 本文由 发表于 2023年7月18日 01:40:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定