0

I have this array of array

a1 = ['one', 'two', ['three', 'four']];

how to get this 'three' and 'four' out of child array and push it in parent array as string like below

a2 = ['one', 'two', 'three, four'];

like this. can I solve it with ES6?

Note: with space between 'three' and 'four' as shown in a2.

1 Answer 1

7

You can use map() and conditionally join() the subarray's values:

const a1 = ['one', 'two', ['three', 'four']];
const a2 = a1.map(v => Array.isArray(v) ? v.join(', ') : v);

console.log(a2);

Sign up to request clarification or add additional context in comments.

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.