3 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以通過(guò)將鍵/值對(duì)解構(gòu)為所需變量來(lái)映射對(duì)象的條目,并為新對(duì)象獲取簡(jiǎn)寫屬性。
var data = { 1: "Baroque", 2: "Glitch Pop ", 3: "Nu Jazz", 4: "Drumfunk", 5: "Bitpop", 6: "Latin Pop", 7: "Carnatic" },
result = Object.entries(data).map(([id, name]) => ({ id, name }));
console.log(result);

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超9個(gè)贊
var obj = {1: "Baroque", 2: "Glitch Pop ", 3: "Nu Jazz", 4: "Drumfunk", 5:
"Bitpop", 6: "Latin Pop", 7: "Carnatic"}
var result = Object.keys(obj).map((key) => [Number(key), obj[key]]);
let output=[]
result.forEach((element)=>{
output.push({
"id":element[0],
"name":element[1]
})
})
console.log('output',output);
您將獲得所需格式的對(duì)象

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超5個(gè)贊
這可以用如下的函數(shù)式編程方式完成,只需一行語(yǔ)句!
Object.keys(source).map(key => ({id: key, name : source[key]}))
找到下面的代碼片段以快速查看結(jié)果
const source = {1: "Baroque", 2: "Glitch Pop ", 3: "Nu Jazz", 4: "Drumfunk", 5: "Bitpop", 6: "Latin Pop", 7: "Carnatic"};
const result = Object.keys(source).map(key => ({id: key, name : source[key]}))
console.log(result)
添加回答
舉報(bào)