What is the best way to merge nested arrays

yury_hr

I have an array of objects that looks like this:

[
 {external_id: 1, items: [{k: 'v'}] },
 {external_id: 2, items: [{k1: 'v1'}] },
 {external_id: 1, items: [{k2: 'v2'}, {k3: 'v3'}] }
]

What I want to do is merge nested arrays based on external_id and return 'clean array' which will look like this:

[
 {external_id: 1, items: [{k: 'v'}, {k2: 'v2'}, {k3: 'v3'}] },
 {external_id: 2, items: [{k1: 'v1'}] }
]

So my question is what is the clean way to achieve that without using classic for-loop ?

trincot

You will have to loop. You can choose to loop with for, forEach, map, reduce, find, ...etc, but you'll have to loop.

Here is a way that creates a Map keyed by the external_id, and then populates that map with the items. Finally the values of that map represent the result:

let data = [{external_id: 1, items: [{k: 'v'}] },{external_id: 2, items: [{k1: 'v1'}] },{external_id: 1, items: [{k2: 'v2'}, {k3: 'v3'}] }];

let map = new Map(data.map(({external_id}) => [external_id, { external_id, items:[] }]));
data.forEach(o => map.get(o.external_id).items.push(...o.items));
let result = [...map.values()];
console.log(result);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the best way to merge 2 byte arrays?

What is the best way to "merge" 2 arrays by each value?

What is the best way to merge hashes?

What is the best way to merge two arrays (element + element), if elements itself are arrays

What's the best way to merge to arrays based on key values in each array?

What is the best way to transform/clean-up an object with nested arrays containing other objects?

What is the best way to Serialize/Deserialize nested Tuple2 arrays in dart (List<List<Tuple2>>)

linux + what the best way to merge files

What is the best way to merge development database with production?

What is the best way to merge multiple Flink DataStreams?

What is the best way to reduce and merge a collection of objects

What is the best way to implement nested dictionaries?

What's the best way to mutate a nested enum?

Angular what is the best way to traverse the nested object

What is the best way to modify params nested hash?

What is a best way to have nested project in Angular

What is the best way to handle nested conditionals in ruby?

What is the best way to validate a nested model with Phoenix?

What is the best way to type such a nested object?

Best way to merge two Arrays of Classes based on Class variable value

Best way to merge and transform keys and arrays from an object

Best way Merge two string arrays based on a condition

What's the best way to document an array of arrays of arrays using JSDoc?

Best way to update nested arrays in C# / MongoDB

lookup and merge nested arrays

What is the best way to merge a feature into an older release branch?

What is the best (and safest) way to merge a Git branch into master?

What is best way to merge detached entities to entity manager in JPA

What is the best way to resolve merge conflicts in .swm files?