VOOZH about

URL: https://qiita.com/es-row/items/b668dccecf86a4404ac5

⇱ 二次元配列を一次元配列にする #JavaScript - Qiita


👁 Image
1

Go to list of users who liked

2

Share on X(Twitter)

Share on Facebook

Add to Hatena Bookmark

More than 5 years have passed since last update.

@es-row(Shiro Sakai)

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

1
Posted at

たとえば、[[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

1

Go to list of users who liked

2
0

Go to list of comments

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1

Go to list of users who liked

2