如果我有此收藏集:
var objArray = [
{ id: 0, name: 'Object 0', otherProp: '321' },
{ id: 1, name: 'O1', otherProp: '648' },
{ id: 2, name: 'Another Object', otherProp: '850' },
{ id: 3, name: 'Almost There', otherProp: '046' },
{ id: 4, name: 'Last Obj', otherProp: '984' }
];
如何获取ID值的数组,示例结果: {0,1,2,3,4}
有什么主意吗?
我一直在搜索它,但我无法获得答案,主要是通过过滤来完成。
答案 0 :(得分:0)
尝试使用reduce。
var objArray = [
{ id: 0, name: 'Object 0', otherProp: '321' },
{ id: 1, name: 'O1', otherProp: '648' },
{ id: 2, name: 'Another Object', otherProp: '850' },
{ id: 3, name: 'Almost There', otherProp: '046' },
{ id: 4, name: 'Last Obj', otherProp: '984' }
];
var res = objArray.reduce(function(out, inp) {
out.push(inp['id']);
return out;
}, []);
console.log(res)