私が持っています:
const fruit = ["Apple", "Orange", "Peer"]
const rank = ["1", "2", "3"]
私が行った場合:
const values = [{
"fruit" : fruit,
"rank" : rank
]}
私は得る
values = [{
{"fruit": ["Apple", "Orange", "Peer"]},
{"rank": ["1", "2", "3"]}
}]
以下の形式でデータを取得するにはどうすればよいですか?
values = [
{ fruit: "Apple", rank : "1"},
{ fruit: "Orange", rank : "2"},
{ fruit: "Peer", rank : "3"},
];
最も簡単なのは、マップを使用して1つの配列をループし、オブジェクトを作成することです。
const fruit = ["Apple", "Orange", "Peer"]
const rank = ["1", "2", "3"]
const result = fruit.map(function(value, index) {
return { fruit: value, rank: rank[index] };
});
console.log(result);
または
const fruit = ["Apple", "Orange", "Peer"]
const rank = ["1", "2", "3"]
const result = fruit.map((fruit, index) => ({ fruit, rank: rank[index] }))
console.log(result);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加