Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
13
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

@yarnaimo

[JavaScript/TypeScript] flat()を使わずに配列をflattenする

[[0, 1], 2, [3, 4]] のようなネストしたりしなかったりする配列をフラットにしたい場合、ES2019 以降だと Array.prototype.flat() が使えるけど Node.js 10 は対応してないので他の方法を考えた。(意外と簡単だった)

const nested = [[0, 1], 2, [3, 4]]
const flat = [].concat(...nested) // => [0, 1, 2, 3, 4]

スプレッド演算子を使うと [].concat([0, 1], 2, [3, 4]) のように呼び出してくれるので結合されてフラットな配列になります。

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
13
Help us understand the problem. What are the problem?