[英]How to pluck key value from object of array
i have object of array like this: 我有这样的数组对象:
var arr = [{id: 1, url: 'something'},{id: 2, url: 'something2'}];
i want output like this : 我想要这样的输出:
[{id:1}, {id:2}]
Thanks in advance 提前致谢
Just use Array#map 只需使用Array#map
const arr = [{id: 1, url: 'something'},{id: 2, url: 'something2'}]; const mapped = arr.map(({id}) => ({ id })); console.log(mapped);
In simple form 简单形式
const arr = [{id: 1, url: 'something'},{id: 2, url: 'something2'}]; const mapped = arr.map(item => { return { id: item.id };}); console.log(mapped);
You should use map function to pluck specific key value from object array. 您应该使用map函数从对象数组中提取特定的键值。
var new_arr= arr.map(function (value, index, array) {
return {"id": value.id};
});
If you can modify the original array, just remove the attr url
如果您可以修改原始数组,只需删除attr
url
var arr = [{id: 1, url: 'something'},{id: 2, url: 'something2'}]; arr.forEach(o => delete o.url); console.log(arr)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.