0

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 !

2
  • Answer below (at least my attempt at it). Please take a look at SO's How to ask page for your future questions though. Thanks! Commented Mar 9, 2018 at 22:06
  • This has nothing to do with TypeScript... Commented Mar 9, 2018 at 22:29

2 Answers 2

1

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));
Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.