JavaScript mapメソッドを使ってオブジェクトの配列を生成する

環境
Windows 10 home 64bit
Google Chrome 107.0.5304.122(Official Build) (64 ビット)

構文
const 配列名1 = [要素1,要素2,…]
const 配列名2 =[要素1,要素2,…]
書式
key:value
{配列名1[インデックス]:配列名2[インデックス]}
mapメソッドを使って2つの配列をそれぞれkeyとvalueにしてオブジェクトの配列を生成します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const city = ['tokyo', 'oosaka', 'fukuoka'];
const nn = [11, 33, 77];
const result = city.map(function(v, i){
return {[city[i]]:nn[i]};
});
console.log(result);
const city = ['tokyo', 'oosaka', 'fukuoka']; const nn = [11, 33, 77]; const result = city.map(function(v, i){ return {[city[i]]:nn[i]}; }); console.log(result);
const city = ['tokyo', 'oosaka', 'fukuoka'];
const nn = [11, 33, 77];

const result = city.map(function(v, i){
  return {[city[i]]:nn[i]};
});

console.log(result);

実行結果
> Array [Object { tokyo: 11 }, Object { oosaka: 33 }, Object { fukuoka: 77 }]

JavaScript

Posted by arkgame