Home > front end >  How to extract array of array values and push in same array in ES6?
How to extract array of array values and push in same array in ES6?

Time:09-21

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. I could do with for loop but its lengthy, can I solve it with ES6?

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

CodePudding user response:

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

  • Related