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' ]
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' ]
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]
const
isn't the way to declare the array.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"]
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);