-4

I have an array of an array of strings like this.

const data =  [ ['a' , 'b'] , ['c' , 'd' , 'e'] , ['f'] ]

I want to convert it into an array of strings like this in javascript.

const data = [ 'a' , 'b' , 'c' , 'd' ]

3 Answers3

1

Look at the docs for this .flat() method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat

const arr1 = [0, 1, 2, [3, 4]];

console.log(arr1.flat());
// expected output: [0, 1, 2, 3, 4]
Though if you're going to modify the array, const isn't the way to declare the array.
1

If you want to update the array you'll have to use let instead of const. This will do it.

let data =  [ ['a' , 'b'] , ['c' , 'd' , 'e'] , ['f'] ]
data = data.flat()
console.log(data)  //["a", "b", "c", "d", "e", "f"]
M.Hassan Nasir
  • 851
  • 2
  • 13
0

You can use concat to convert this array

var array =  [ ['a' , 'b'] , ['c' , 'd' , 'e'] , ['f'] ];
var UpdatedArray = [];


for(var i = 0; i < array.length; i++)
{
    UpdatedArray = UpdatedArray.concat(array[i]);
}

console.log(UpdatedArray);
Wajid
  • 593
  • 3
  • 11