英文:
Merging and Tranforming Array of object in javascripts
问题
我有两个对象数组,如下所示...
const planned = [
{
'2023-01-06': 46,
'2023-01-04': 45,
'2023-01-05': 43,
'2023-01-07': 53
}
]
const actual = [
{
"2023-01-07": 12,
"2023-01-06": 16,
"2023-01-04": 14,
"2023-01-08": 10,
"2023-01-05": 12,
"2023-01-03": 10
}
]
我尝试将它们合并成一个包含日期和值的对象数组,这是我期望的数组:
```javascript
const transformed = [{
date: "2023-01-03",
actual: 10,
planned: 0,
},{
date: "2023-01-05",
actual: 12,
planned: 43,
},{
date: "2023-01-06",
actual: 16,
planned: 46,
},....
]
这是我的尝试:
```javascript
const transformed = planned.map((el)=>{
let obj ={}
actual.map((els)=>{
if(el.key === els.key){
obj["date"]=== el.key
obj["actual"]=== el.keys
obj["planned"]=== el.key
}
})
return {...obj}
})
我尝试弄清楚,但在如何创建逻辑方面有一些问题... 有人能给我一些提示吗?我们可以使用reduce原型数组或任何内置的方法吗?对此的任何帮助都将非常感激。
英文:
So i have two array of object like this...
const planned = [
{
'2023-01-06': 46,
'2023-01-04': 45,
'2023-01-05': 43,
'2023-01-07': 53
}
]
const actual =[
{
"2023-01-07": 12,
"2023-01-06": 16,
"2023-01-04": 14,
"2023-01-08": 10,
"2023-01-05": 12,
"2023-01-03": 10
}
]
i try to merge it into one array of object that separated the date and the value, here is my expected array:
const transformed = [{
date:"2023-01-03",
actual:10,
planned:0,
},{
date:"2023-01-05",
actual:10,
planned:5,
},{
date:"2023-01-06",
actual:16,
planned:46,
},....
]
here is my try:
const transformed = planned.map((el)=>{
let obj ={}
actual.map((els)=>{
if(el.key === els.key){
obj["date"]=== el.key
obj["actual"]=== el.keys
obj["planned"]=== el.key
}
})
return {...obj}
})
i try to figure it out, but have some issues on how to create the logic... anyone here to give me clue on it? can we use reduce prototype array or any built in?, any help on this will be verry thankful
答案1
得分: 2
这是一种方法。 planned
和 actual
是数组,所以我们首先将它们转换为一个扁平映射,然后使用这个映射创建输出。
const planned = [
{
"2023-01-06": 46,
"2023-01-04": 45,
"2023-01-05": 43,
"2023-01-07": 53,
},
];
const actual = [
{
"2023-01-07": 12,
"2023-01-06": 16,
"2023-01-04": 14,
"2023-01-08": 10,
"2023-01-05": 12,
"2023-01-03": 10,
},
];
const convertToMap = (list) =>
list.reduce((acc, curr) => ({ ...acc, ...curr }), {});
const actualStats = convertToMap(actual);
const plannedStats = convertToMap(planned);
const dates = [
...new Set([...Object.keys(actualStats), ...Object.keys(plannedStats)]),
];
const transformed = dates.map((date) => ({
date,
planned: plannedStats[date] || 0,
actual: actualStats[date] || 0,
}));
console.log(transformed);
如果您需要更多帮助,请告诉我。
英文:
Here's an approach. planned
and actual
are arrays, so we first convert them to a flat map and then use this map to create the output
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const planned = [
{
"2023-01-06": 46,
"2023-01-04": 45,
"2023-01-05": 43,
"2023-01-07": 53,
},
];
const actual = [
{
"2023-01-07": 12,
"2023-01-06": 16,
"2023-01-04": 14,
"2023-01-08": 10,
"2023-01-05": 12,
"2023-01-03": 10,
},
];
const convertToMap = (list) =>
list.reduce((acc, curr) => ({ ...acc, ...curr }), {});
const actualStats = convertToMap(actual);
const plannedStats = convertToMap(planned);
const dates = [
...new Set([...Object.keys(actualStats), ...Object.keys(plannedStats)]),
];
const transformed = dates.map((date) => ({
date,
planned: plannedStats[date] || 0,
actual: actualStats[date] || 0,
}));
console.log(transformed);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论