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

[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]) のように呼び出してくれるので結合されてフラットな配列になります。

yarnaimo
個人でWebサービスなどを作っています。 Webデザイン / TypeScript / Firebase / React / Next.js / Node.js
admin-guild
「Webサービスの運営に必要なあらゆる知見」を共有できる場として作られた、運営者のためのコミュニティです。
https://admin-guild.slack.com
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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした