如何从数组对象中提取键值

How to pluck key value from object of array

我有这样的数组对象:

var arr = [{id: 1, url: 'something'},{id: 2, url: 'something2'}];

我想要这样的输出:

[{id:1}, {id:2}]

提前致谢

只需使用Array#map

const arr = [{id: 1, url: 'something'},{id: 2, url: 'something2'}];
const mapped = arr.map(({id}) => ({ id }));

console.log(mapped);

简单形式

const arr = [{id: 1, url: 'something'},{id: 2, url: 'something2'}];
const mapped = arr.map(item => { return { id: item.id };});

console.log(mapped);

您应该使用 map 函数从对象数组中提取特定的键值。

var new_arr= arr.map(function (value, index, array) {

   return {"id": value.id}; 

});

如果可以修改原数组,去掉attr即可url

var arr = [{id: 1, url: 'something'},{id: 2, url: 'something2'}];
arr.forEach(o => delete o.url);
console.log(arr)