es6の多次元配列を1次元配列に変換する方法は何ですか?
ES6では、スプレッド演算子とArray.prototype.concat()メソッドを使用して、多次元配列を一次元配列に変換できます。
スプレッド演算子を使用します。
const multidimensionalArray = [[1, 2], [3, 4], [5, 6]];
const flattenedArray = [].concat(...multidimensionalArray);
console.log(flattenedArray);
// Output: [1, 2, 3, 4, 5, 6]
Array.prototype.concat()メソッドを使う。
const multidimensionalArray = [[1, 2], [3, 4], [5, 6]];
const flattenedArray = [].concat.apply([], multidimensionalArray);
console.log(flattenedArray);
// Output: [1, 2, 3, 4, 5, 6]
これらの方法のどちらも、多次元配列のすべての要素を1次元配列に結合することができます。