[英]How to get property from object array?
I have this array: 我有这个数组:
var arr1 = [{id:124,name:'qqq'},
{id:589,name:'www'},
{id:45,name:'eee'},
{id:567,name:'rrr'}]
I need to get all Id's. 我需要获取所有ID。
var Id's = [124,589,45,567];
What's the elegant way to retrieve all id's property from object array? 从对象数组检索所有id的属性的优雅方法是什么?
The
map()
method creates a new array with the results of calling a provided function on every element in this array.map()
方法创建一个新数组,并对该数组中的每个元素调用提供的函数。
var arr1 = [{ id: 124, name: 'qqq' }, { id: 589, name: 'www' }, { id: 45, name: 'eee' }, { id: 567, name: 'rrr' }]; var op = arr1.map(function(item) { return item.id; }); //Using Arrow functions `arr1.map((item) => (item.id));` console.log(op);
尝试这个
var output = arr1.map(function(obj){ return obj.id; })
var result = arr1.map(function(obj) {
return obj.id
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.