3 回答
TA贡献1828条经验 获得超6个赞
您可以通过将键/值对解构为所需变量来映射对象的条目,并为新对象获取简写属性。
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贡献1805条经验 获得超9个赞
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);
您将获得所需格式的对象
TA贡献1811条经验 获得超5个赞
这可以用如下的函数式编程方式完成,只需一行语句!
Object.keys(source).map(key => ({id: key, name : source[key]}))
找到下面的代码片段以快速查看结果
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)
添加回答
举报