marcXandre
marcXandre

Reputation: 2432

Merge array who is inside an object in Typescript

I need to merge two object, with an array inside, together :

res : [ { data: [1,2,3] }, { data: [4,5,6] } ]

And the result will look like this :

res : [1,2,3,4,5,6]

How can I get there ?

Thank you !

Upvotes: 0

Views: 90

Answers (2)

Behrooz
Behrooz

Reputation: 2371

You can use Array.map as follows:

const input = [{ data: [1, 2, 3] }, { data: [4, 5, 6] }];
const output = [].concat(...input.map((item) => item.data));

Upvotes: 1

Jeto
Jeto

Reputation: 14927

You can use Array.reduce along with Array.concat:

let input = [ { data: [1,2,3] }, { data: [4,5,6] } ];

let result = input.reduce((result, entry) => result.concat(entry.data), []);

console.log(result);

Upvotes: 1

Related Questions