英文:
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 => {
return Object.entries(obj).map(([date, count]) => {
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 = [
{ '2023-06-02': 2, '2023-06-21': 6 },
{ '2023-06-29': 2, '2023-06-23': 1 }
]
const arrayTransformed = arrayWithDates.map(record =>
Object.entries(record).map(([date, count]) =>
({ count: `${count}`, date })));
console.log(arrayTransformed);
<!-- language: lang-css -->
.as-console-wrapper { top: 0; max-height: 100% !important; }
<!-- end snippet -->
Output
[
[
{ "count": "2", "date": "2023-06-02" },
{ "count": "6", "date": "2023-06-21" }
],
[
{ "count": "2", "date": "2023-06-29" },
{ "count": "1", "date": "2023-06-23" }
]
]
If you want to flatten the data, call flat()
.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const arrayWithDates = [
{ '2023-06-02': 2, '2023-06-21': 6 },
{ '2023-06-29': 2, '2023-06-23': 1 }
]
const arrayTransformed = arrayWithDates.map(record =>
Object.entries(record).map(([date, count]) =>
({ count: `${count}`, date }))).flat();
console.log(arrayTransformed);
<!-- language: lang-css -->
.as-console-wrapper { top: 0; max-height: 100% !important; }
<!-- end snippet -->
Output
[
{ "count": "2", "date": "2023-06-02" },
{ "count": "6", "date": "2023-06-21" }
{ "count": "2", "date": "2023-06-29" },
{ "count": "1", "date": "2023-06-23" }
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论