Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何以角度映射数组中的数据_Javascript_Arrays_Json - Fatal编程技术网

Javascript 如何以角度映射数组中的数据

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

我试图从数组中提取数据,但我只想提取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"}]
结果:

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))