从数组中的对象过滤键[重复]

时间:2022-09-25 07:58:07

This question already has an answer here:

这个问题在这里已有答案:

My API hands me this array of objects:

我的API递给我这个对象数组:

[
 { id: 5, name: "foo" },
 { id: 7, name: "bar" }
]

I would like to filter out the ID keys and achieve this:

我想过滤掉ID键并实现这个目的:

[5,7]

What would be considered best practice in this case?

在这种情况下,什么是最佳做法?

1 个解决方案

#1


-1  

Just use array.map:

只需使用array.map:

var data = [
 { id: 5, name: "foo" },
 { id: 7, name: "bar" }
];

var res = data.map(({id}) => id);

console.log(res);

#1


-1  

Just use array.map:

只需使用array.map:

var data = [
 { id: 5, name: "foo" },
 { id: 7, name: "bar" }
];

var res = data.map(({id}) => id);

console.log(res);