Javascript 如何以角度映射数组中的数据
我试图从数组中提取数据,但我只想提取id字段,而不是name字段。使用Json Stringify,我可以在控制台中记录结果,下面是代码和结果:Javascript 如何以角度映射数组中的数据,javascript,arrays,json,Javascript,Arrays,Json,我试图从数组中提取数据,但我只想提取id字段,而不是name字段。使用Json Stringify,我可以在控制台中记录结果,下面是代码和结果: console.log(JSON.stringify(this.selectedCustomers.id)); 控制台日志: console.log(JSON.stringify(this.selectedCustomers)); [{"id":1,"name":"John"},{"id":2,"name":"Jane"}] 结果: consol
console.log(JSON.stringify(this.selectedCustomers.id));
控制台日志:
console.log(JSON.stringify(this.selectedCustomers));
[{"id":1,"name":"John"},{"id":2,"name":"Jane"}]
结果:
console.log(JSON.stringify(this.selectedCustomers));
[{"id":1,"name":"John"},{"id":2,"name":"Jane"}]
期望的结果应该是:[1,2]
我尝试了以下方法,认为它会过滤结果:
console.log(JSON.stringify(this.selectedCustomers.id));
但我收到了以下错误消息:“属性'id'在类型'any[]上不存在”
如何去掉name字段并直接提取id?如果有人能告诉我我错过了什么,或者如何取回身份证,我将不胜感激!谢谢 您需要使用
map
方法和解构
简短示例:
let arr=[{“id”:1,“name”:“John”},{“id”:2,“name”:“Jane”}]
log(arr.map(({id})=>id))
您需要使用map
方法和解构
简短示例:
let arr=[{“id”:1,“name”:“John”},{“id”:2,“name”:“Jane”}]
log(arr.map(({id})=>id))代码>