忍者ブログ
情報処理試験など、理系の試験対策、関連知識、日記などです

JavaScript MAP



キーと値の対応付けを管理するデータ構造です。

1.サンプル



let Users = new Map ([

  ["id1", "id1の名前"] ,

  ["id2", "id2の名前"] ,

  ["id3", "id3の名前"] ,

]);



// 要素を一つずつ表示

console.log(Users.get("id1")) ;



//要素を全部表示

for (let [key , value] of Users){

    console.log(key + "=" + value) ;

}



//要素を全部表示

Users.forEach(function(value, key){

    console.log(key + "=" + value) ;

})

2.実行結果

id1の名前

id1=id1の名前

id2=id2の名前

id3=id3の名前

id1=id1の名前

id2=id2の名前

id3=id3の名前



と表示されます。










PR