This question already has an answer here:
这个问题在这里已有答案:
- From an array of objects, extract value of a property as array 12 answers
从一组对象中,提取属性的值作为数组12的答案
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);