[英]Map array inside object inside array into a single array
我有一個對象數組,其中每個對象都有一個屬性,其值是另一個數組,就像這樣
[
{
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
}
]
我嘗試使用像newArr: arr.reduce((a,b) => a.parts.concat(b.parts))
這樣的newArr: arr.reduce((a,b) => a.parts.concat(b.parts))
但得到錯誤“無法讀取未定義的屬性連接”。 我還嘗試將 reduce 的 initialValue 參數作為空數組提供,但錯誤相同。
作為獎勵,我最終需要最終的數組不包含重復的部分:即如果一個部分在兩個位置,它只會合並數量。
對使用es6的解決方案開放但不需要修改原始數組
該解決方案使用reduce和forEach將子數組中的所有對象組合成一個Map ,以part_d
為鍵, qty
是組合qty
的結果。 當找到新的part_id
,通過將部件擴展到新對象並用 0 覆蓋qty
來創建新對象。
然后通過傳播Map.values()
迭代器將 Map 轉換回數組:
const data = [{"location":"somewhere","location_id":1,"parts":[{"part_id":1,"description":"foo","qty":1}]},{"location":"somewhere2","location_id":2,"parts":[{"part_id":1,"description":"foo","qty":3},{"part_id":2,"description":"bar","qty":1}]}]; const result = [... data.reduce((r, { parts }) => { (parts || []).forEach((o) => { r.has(o.part_id) || r.set(o.part_id, { ...o, qty: 0 }); r.get(o.part_id).qty += o.qty; }); return r; }, new Map()) .values()]; console.log(result);
如果您確定要映射數組而不是其中的對象:
const input = [ { location: 'somewhere', location_id: 1, parts: [ { part_id: 1, description: 'something' } ] }, { location: 'somewhere2', location_id: 22, parts: [ { part_id: 2, description: 'something' } ] } ]; const mapped = input.map(outerObj => outerObj.parts[0]); console.log(mapped);
這會將所有零件對象添加到基礎中並刪除零件數組。 它將修改舊對象。
let arr = [ { location: 'somewhere', location_id: 1, parts: [ { part_id: 1, description: 'something' } ] } ] arr.map(m => { m.parts.forEach(p => Object.assign(m, p)) delete m.parts; }) console.log(arr);
您可以使用array#reduce
首先加入每個對象的所有parts
數組。 然后使用array#reduce
在part_id
和description
上對結果數組中的每個對象進行分組,並總結每個唯一對象的qty
。 然后從這個對象中獲取所有值。
const input = [ { location: 'somewhere', location_id: 1, parts: [ { part_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_id: 2, description: 'something', qty: 4 } ] } ], result = Object.values(input.reduce((r, {parts}) => r.concat(parts),[]) .reduce((r,o) => { r[`${o.part_id}_${o.description}`] = r[`${o.part_id}_${o.description}`] || {...o, qty: 0}; r[`${o.part_id}_${o.description}`].qty += o.qty; return r; },{})); console.log(result);
這個問題可能有兩種情況:
每個位置在部分數組下都有一個對象。
演示
let jsonObj = [ { location: 'somewhere', location_id: 1, parts: [ { part_id: 1, description: 'foo', qty: 1, } ] }, { location: 'somewhere', location_id: 2, parts: [ { part_id: 2, description: 'foo', qty: 1, } ] } ]; let res = jsonObj.map(obj => obj.parts[0]); console.log(res);
單個位置在部分數組下有多個對象。
演示
let jsonObj = [ { location: 'somewhere', location_id: 1, parts: [ { part_id: 1, description: 'foo', qty: 1, }, { part_id: 2, description: 'foo', qty: 1, } ] } ]; let res = jsonObj[0].parts; console.log(res);
我也有類似的情況。 對我array.push(part)
是定義一個新數組,然后從雙映射函數內部定義array.push(part)
:
const input = [{
location: 'somewhere',
location_id: 1,
parts: [{
part_id: 1,
description: 'something'
},
{
part_id: 2,
description: 'something'
}
]
},
{
location: 'somewhere2',
location_id: 22,
parts: [{
part_id: 3,
description: 'something'
},
{
part_id: 4,
description: 'something'
}
]
}
];
如果這是您的輸入..
var list = []
input.map(item => {
item.parts.map(part => {
list.push(part);
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.