Javascript 将数组中的对象映射到单个数组中
我有一个对象数组,其中每个对象都有一个属性,其值是另一个数组,如下所示Javascript 将数组中的对象映射到单个数组中,javascript,arrays,Javascript,Arrays,我有一个对象数组,其中每个对象都有一个属性,其值是另一个数组,如下所示 [ { location: 'somewhere', location_id: 1, parts: [ { part_id: 1, description: 'foo', qty: 1, } ] } ] [ { part_id: 1, description: 'foo', qty:
[
{
location: 'somewhere',
location_id: 1,
parts: [
{
part_id: 1,
description: 'foo',
qty: 1,
}
]
}
]
[
{
part_id: 1,
description: 'foo',
qty: 1
},
{
part_id: 2,
description: 'bar',
qty: 1
}
]
我需要把这些数组映射成这样一个数组
[
{
location: 'somewhere',
location_id: 1,
parts: [
{
part_id: 1,
description: 'foo',
qty: 1,
}
]
}
]
[
{
part_id: 1,
description: 'foo',
qty: 1
},
{
part_id: 2,
description: 'bar',
qty: 1
}
]
我试着用reduce-like
newArr:arr.reduce((a,b)=>a.parts.concat(b.parts))
但出现错误“无法读取未定义的属性concat”。我还尝试将reduce的initialValue参数作为空数组提供,但出现相同错误
作为奖励,我最终需要最终数组不包含零件的副本:即,如果一个零件位于两个位置,它将只合并数量
打开使用es6的解决方案,但如果确定要映射阵列而不是其中的对象,则不需要修改原始阵列:
const输入=[
{
地点:'某处',
地点_id:1,,
部分:[
{
第1部分:,
描述:“某物”
}
]
},
{
地点:'somewhere 2',
地点_id:22,,
部分:[
{
第二部分:,
描述:“某物”
}
]
}
];
const-mapped=input.map(outerObj=>outerObj.parts[0]);
console.log(映射)代码>如果确定要映射数组而不是其中的对象,请执行以下操作:
const输入=[
{
地点:'某处',
地点_id:1,,
部分:[
{
第1部分:,
描述:“某物”
}
]
},
{
地点:'somewhere 2',
地点_id:22,,
部分:[
{
第二部分:,
描述:“某物”
}
]
}
];
const-mapped=input.map(outerObj=>outerObj.parts[0]);
console.log(映射)代码>此操作将所有零件对象添加到基础中并删除零件阵列。它将修改旧对象
让arr=[
{
地点:'某处',
地点_id:1,,
部分:[
{
第1部分:,
描述:“某物”
}
]
}
]
arr.map(m=>{
m、 parts.forEach(p=>Object.assign(m,p))
删除m部分;
})
控制台日志(arr)代码>此操作将所有零件对象添加到基础中并删除零件阵列。它将修改旧对象
让arr=[
{
地点:'某处',
地点_id:1,,
部分:[
{
第1部分:,
描述:“某物”
}
]
}
]
arr.map(m=>{
m、 parts.forEach(p=>Object.assign(m,p))
删除m部分;
})
控制台日志(arr)
您可以使用array#reduce
首先将每个对象的部分
数组连接起来。然后使用array#reduce
将结果数组中的每个对象分组到part\u id
和description
上,并汇总每个唯一对象的数量。然后从该对象获取所有值
const input=[{location:'somewhere',location:'id:1,description:'foo',qty:1}]},{location:'somewhere2',location:'id:22,parts:[{part:'id:2,description:'something',qty:3}},{location:'somewhere2',location:'id:22,parts:[{part part part id:2,description:'something',qty:4}]],
result=Object.values(input.reduce((r,{parts})=>r.concat(parts),[]))
.减少((r,o)=>{
r[`{o.part_-id}{o.description}`]=r[`{o.part_-id}}{o.description}`]|{…o,数量:0};
r[`{o.part\u id}{u${o.description}`].qty+=o.qty;
返回r;
},{}));
控制台日志(结果)
您可以使用array#reduce
首先将每个对象的部分
数组连接起来。然后使用array#reduce
将结果数组中的每个对象分组到part\u id
和description
上,并汇总每个唯一对象的数量。然后从该对象获取所有值
const input=[{location:'somewhere',location:'id:1,description:'foo',qty:1}]},{location:'somewhere2',location:'id:22,parts:[{part:'id:2,description:'something',qty:3}},{location:'somewhere2',location:'id:22,parts:[{part part part id:2,description:'something',qty:4}]],
result=Object.values(input.reduce((r,{parts})=>r.concat(parts),[]))
.减少((r,o)=>{
r[`{o.part_-id}{o.description}`]=r[`{o.part_-id}}{o.description}`]|{…o,数量:0};
r[`{o.part\u id}{u${o.description}`].qty+=o.qty;
返回r;
},{}));
控制台日志(结果)
此解决方案使用并将子数组中的所有对象组合成一个数组,部分d
作为键,数量
是组合数量的结果。当找到新的零件id
时,零件将创建一个新对象,并将数量
覆盖为0
然后迭代器将映射转换回数组:
const data=[{“位置”:“某处”,“位置id”:1,“零件”:[{“零件id”:1,“描述”:“foo”,“数量”:1}],{“位置”:“某地2”,“位置id”:2,“零件”:[{“零件id”:1,“描述”:“foo”,“数量”:3},{“零件id”:2,“描述”:“条”,“数量”:1}];
常量结果=[。。。
data.reduce((r,{parts})=>{
(部分| |[])。forEach((o)=>{
r、 has(o.part_id)| r.set(o.part_id,{…o,qty:0});
r、 获取(o.part_id)。数量+=o.qty;
});
返回r;
},新映射())
.values()];
控制台日志(结果)
此解决方案使用并将子数组中的所有对象组合成一个数组,部分d
作为键,数量
是组合数量的结果。当找到新的零件id
时,零件将创建一个新对象,并将数量
覆盖为0
然后迭代器将映射转换回数组:
const data=[{“位置”:“某处”,“位置id”:1,“零件”:[{“零件id”:1,“描述”:“foo”,“数量”:1}],{“位置”:“某地2”,“位置id”:2,“零件”:[{“零件id”:1,“描述”:“foo”,“数量”:3},{“零件id”:2,“描述”:“条”,“数量”:1}];
常数结果