配列初期化

10要素中7要素1を設定し残り0を設定。

const data = Array.from({ length: 10 }, (_, i) => i < 7 ? 1 : 0);

console.log(data); //Array(10) [ 1, 1, 1, 1, 1, 1, 1, 0, 0, 0]



Array.from() Array.from() 静的メソッドは、反復可能オブジェクトや配列風オブジェクトからシャローコピーされた、新しい Array インスタンスを生成します。 mdn web docs

console.log(Array.from('foo'));
// Expected output: Array ["f", "o", "o"]

console.log(Array.from([1, 2, 3], (x) => x + x));
// Expected output: Array [2, 4, 6]

console.log(Array.from({length: 5}));
// Expected output: Array [undefined, undefined, undefined, undefined, undefined]
投稿日時: 2024-07-28 05:43:28
更新日時: 2024-07-28 07:27:28

最近の投稿

タグ

アーカイブ

その他