gpt4 book ai didi

javascript - 在javascript中将对象数组的某些字段转换为json数据?

转载 作者:行者123 更新时间:2023-11-28 18:15:46 30 4
gpt4 key购买 nike

我有一个如下所述的对象数组,

  var arraydata =[
{ id:1, name:"Abraham", age:20,gender:"male"},
{ id:2, name:"Annie", age:25,gender:"female"},
{ id:3, name:"Ryan", age:40,gender:"male"},
{ id:4, name:"Wayne", age:31,gender:"male"},
{ id:5, name:"Paul", age:45,gender:"male"}
];

如何将其转换为只有“id”和“name”字段的 JSON 数据?

最佳答案

语法错误age=20,gender="male"将其更改为age:20,gender:"male"

var arraydata =[
{ id:1, name:"Abraham", age: 20, gender:"male"},
{ id:2, name:"Annie", age: 25, gender:"female"},
{ id:3, name:"Ryan", age: 28, gender:"male"},
{ id:4, name:"Wayne", age: 19, gender:"male"},
{ id:5, name:"Paul", age: 45,gender:"male"}
];
var ans= arraydata.map(function(a){
return {id:a.id,name:a.name};
})
console.log(ans);

关于javascript - 在javascript中将对象数组的某些字段转换为json数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40785222/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com