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

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

Split values from array with objects to a different object

问题

  1. 基本上我有这个数据
  2. ```javascript
  3. const arrayWithDates = [{2023-06-02: 2, 2023-06-21: 6}, {2023-06-29: 2, 2023-06-23: 1}]

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

  1. const arrayTransformed = [
  2. [{count: '2', date: '2023-06-02'}, {count: '6', date: '2023-06-21'}],
  3. [{count: '2', date: '2023-06-29'}, {count: '1', date: '2023-06-23'}]
  4. ]
  1. <details>
  2. <summary>英文:</summary>
  3. Basically I have this data:

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

  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'}]]

  1. </details>
  2. # 答案1
  3. **得分**: 0
  4. 假设您的`arrayWithDates`中的日期是以字符串格式化的,那么以下代码应该可以工作:
  5. ```javascript
  6. const arrayTransformed = arrayWithDates.map(obj => {
  7. return Object.entries(obj).map(([date, count]) => {
  8. return { date, count: count.toString() };
  9. });
  10. });
英文:

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

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

答案2

得分: 0

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

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

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

输出:

  1. [
  2. [
  3. { "count": "2", "date": "2023-06-02" },
  4. { "count": "6", "date": "2023-06-21" }
  5. ],
  6. [
  7. { "count": "2", "date": "2023-06-29" },
  8. { "count": "1", "date": "2023-06-23" }
  9. ]
  10. ]
  1. [
  2. { "count": "2", "date": "2023-06-02" },
  3. { "count": "6", "date": "2023-06-21" },
  4. { "count": "2", "date": "2023-06-29" },
  5. { "count": "1", "date": "2023-06-23" }
  6. ]
英文:

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 -->

  1. const arrayWithDates = [
  2. { &#39;2023-06-02&#39;: 2, &#39;2023-06-21&#39;: 6 },
  3. { &#39;2023-06-29&#39;: 2, &#39;2023-06-23&#39;: 1 }
  4. ]
  5. const arrayTransformed = arrayWithDates.map(record =&gt;
  6. Object.entries(record).map(([date, count]) =&gt;
  7. ({ count: `${count}`, date })));
  8. console.log(arrayTransformed);

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

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

<!-- end snippet -->

Output

  1. [
  2. [
  3. { &quot;count&quot;: &quot;2&quot;, &quot;date&quot;: &quot;2023-06-02&quot; },
  4. { &quot;count&quot;: &quot;6&quot;, &quot;date&quot;: &quot;2023-06-21&quot; }
  5. ],
  6. [
  7. { &quot;count&quot;: &quot;2&quot;, &quot;date&quot;: &quot;2023-06-29&quot; },
  8. { &quot;count&quot;: &quot;1&quot;, &quot;date&quot;: &quot;2023-06-23&quot; }
  9. ]
  10. ]

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

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

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

  1. const arrayWithDates = [
  2. { &#39;2023-06-02&#39;: 2, &#39;2023-06-21&#39;: 6 },
  3. { &#39;2023-06-29&#39;: 2, &#39;2023-06-23&#39;: 1 }
  4. ]
  5. const arrayTransformed = arrayWithDates.map(record =&gt;
  6. Object.entries(record).map(([date, count]) =&gt;
  7. ({ count: `${count}`, date }))).flat();
  8. console.log(arrayTransformed);

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

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

<!-- end snippet -->

Output

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

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:

确定