Home > Back-end >  How can I convert an array with two sets of brackets to have only one set of brackets?
How can I convert an array with two sets of brackets to have only one set of brackets?

Time:06-23

how can I convert [[" ", " ", " ", " ", " ", " ", " ", " ", " ", " "]] to

[" ", " ", " ", " ", " ", " ", " ", " ", " ", " "]?

I pushed an array to another, now I want to remove outer brackets from ii .

I'm looking for a method.

let ii :any[]=[];
const i =  ' '.repeat(10) ;
const d =  i.split('')   ; 
const aa=ii.push(d);
console.log(ii,ii.length);
console.log(d,d.length ); 

CodePudding user response:

Modern javascript enviroments support the flat() method.

const flatData = [["a", "b", ["c", "d"]]].flat(2)
console.log(flatData)
// ["a", "b", "c", "d"]

The number you can optionally pass to flat() is the number of levels to flatten.

  • Related