How can a multidimensional array in ES6 be converted into a one-dimensional array?
In ES6, the spread operator and Array.prototype.concat() method can be used to convert multidimensional arrays into one-dimensional arrays.
Using the spread operator:
const multidimensionalArray = [[1, 2], [3, 4], [5, 6]];
const flattenedArray = [].concat(...multidimensionalArray);
console.log(flattenedArray);
// Output: [1, 2, 3, 4, 5, 6]
Utilize the Array.prototype.concat() method:
const multidimensionalArray = [[1, 2], [3, 4], [5, 6]];
const flattenedArray = [].concat.apply([], multidimensionalArray);
console.log(flattenedArray);
// Output: [1, 2, 3, 4, 5, 6]
Both methods can combine all elements of a multi-dimensional array into a new one-dimensional array.