LoginSignup
1
1

More than 5 years have passed since last update.

Javascriptの配列mapメモ

Posted at

JavaScriptの配列におけるmapとは

配列の全部の要素mapの引数に与えたコールバック関数を適用 => 各要素がここでの結果に変換された配列を生成する。

map() メソッドは、与えられた関数を配列のすべての要素に対して呼び出し、その結果からなる新しい配列を生成します。

引用元
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/map

実装してみた

let numArray = [1, 2, 3];
let strArray = numArray.map((move) => {
    switch (move) {
        case 1:
            return 'one';
        case 2:
           return 'two';
        default:
            return  'other';
    }
});
console.log(`map後の配列は、${strArray}`);

実行結果

map後の配列は、one,two,other

やっていること

mapを通して、numArrayの全要素(コードだとmove)にswitchをしている。

実際のコード

わかりにくいので、 console.logwindow.alert に変更している

Playground · TypeScript

参考にさせていただいたサイト

Array.prototype.map() - JavaScript | MDN

JavaScriptでforEach, filter, map, reduceとか - Qiita

1
1
0

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
1