admin管理员组文章数量:1356591
Given the object below, I'm looking for a clean and efficient way to create an array from each item's "subItems" array.
const items = [{
'a': 'a1',
'b': 'b1',
'subItems': [
{'c': 'c1'},
{'d': 'd1'}
]
},
{
'e': 'e1',
'subItems': [
{'f': 'f1'},
{'g': 'g1'}
]
}
];
So for this example, the result I would need is:
const groupedSubItems = [{c: c1},{d:d1},{f:f1},{g:g1}];
Currently I'm just looping through and concatting all the arrays but it does not feel like the best way to handle this:
let groupedSubItems = [];
items.forEach(function(item) {
if (item.subItems) {
groupedSubItems = groupedSubItems.concat(item.subItems);
}
});
Note: All subitems arrays will only be one level down as shown, with the same key, and may or may not exist.
Given the object below, I'm looking for a clean and efficient way to create an array from each item's "subItems" array.
const items = [{
'a': 'a1',
'b': 'b1',
'subItems': [
{'c': 'c1'},
{'d': 'd1'}
]
},
{
'e': 'e1',
'subItems': [
{'f': 'f1'},
{'g': 'g1'}
]
}
];
So for this example, the result I would need is:
const groupedSubItems = [{c: c1},{d:d1},{f:f1},{g:g1}];
Currently I'm just looping through and concatting all the arrays but it does not feel like the best way to handle this:
let groupedSubItems = [];
items.forEach(function(item) {
if (item.subItems) {
groupedSubItems = groupedSubItems.concat(item.subItems);
}
});
Note: All subitems arrays will only be one level down as shown, with the same key, and may or may not exist.
Share Improve this question asked Nov 20, 2018 at 13:51 DasBeastoDasBeasto 2,2926 gold badges28 silver badges72 bronze badges4 Answers
Reset to default 6You can use concat
with spread syntax and map
method to create array of subitems.
const items = [{"a":"a1","b":"b1","subItems":[{"c":"c1"},{"d":"d1"}]},{"e":"e1","subItems":[{"f":"f1"},{"g":"g1"}]}]
const subItems = [].concat(...items.map(({subItems}) => subItems || []));
console.log(subItems)
First you want to get only the subitems:
var itemsSubItems = items.map(function(item) { return item.subItems; });
// [ [ { item: 1 }, { item: 2 } ], [ { item: 3 } ] ]
This gives you an array of arrays. Each element in the array still represents an item: the sub items of that item. You now want just the array of subitems though, not delimited by item.
var subItems = itemsSubItems.reduce(function(si, current) {
return si.concat(current);
}, []);
For each array-of-subitems in the outer array, concatenate them to a new array.
A shorter ES6 example:
const subItems = items.map(item => item.subItems).reduce((a, c) => a.concat(c), []);
You can easily achieve this by applying reduce over your array of items.
const items = [{"a":"a1","b":"b1","subItems":[{"c":"c1"},{"d":"d1"}]},{"e":"e1","subItems":[{"f":"f1"},{"g":"g1"}]}];
const subItems = items.reduce((acc, actual) => acc.concat(actual['subItems']||[]),[]);
you can use forEach
and spread operator ...
to achieve desired result
const items = [{'a': 'a1','b': 'b1','subItems': [{'c': 'c1'},{'d': 'd1'}]},{'e': 'e1','subItems': [{'f': 'f1'},{'g': 'g1'}]}];
const subItems = [];
items.forEach(obj => subItems.push(...obj.subItems));
console.log(subItems);
本文标签: javascriptMerge object nested arrays into a single new arrayStack Overflow
版权声明:本文标题:javascript - Merge object nested arrays into a single new array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744067106a2585203.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论