Help us understand the problem. What is going on with this article?

二次元配列を一次元配列にする

More than 1 year has passed since last update.

たとえば、[[1,2,3],[5, 6, 7]] という二次元配列があったとして、これを [1, 2, 3, 4, 5, 6, 7] というふうにひとつの配列にまとめたい場合、Array.reduce を使えば簡潔に書くことができる。

const arrayOfArray = [[1,2,3],[5, 6, 7]];
const flattenedArray = arrayOfArray.reduce((acc, eachArray) => acc.concat(eachArray), []);
console.log(flattenedArray); //[1, 2, 3, 4, 5, 6, 7]

MDN Array.reduce

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away