对象数组中,每个对象有多个属性,获取其中的一个属性 返回一个新数组

1.给定一个数组 

  series:[
    {name: "使用中资源量",value: 754},
    {name: "维修中资源量",value: 611},
    {name: "保养中资源量",value: 400},
    {name: "已损坏资源量",value: 200}
  ]

2.获取name/value属性 希望得到value属性的数组

  arrValue:[754,611,400,200]

3.定义获取方法

  

getArrayProps(array, key) {
var key = key || "value";
var res = [];
if (array) {
array.forEach(function(t) {
res.push(t[key]);
});
}
return res;
}


4.方法调用

 

   var arrValue = getArrayProps(series,'value');
   console.log(arrValue);
   var arrName = getArrayProps(series,'name');
   console.log(arrName);

5.打印结果


原文地址:https://www.cnblogs.com/iamlhr/p/11498252.html