需要收集对象数组中的每个 属性 值来分隔数组

need to collect each property values in an array of objects to separate arrays

我需要收集每个 属性 对象数组中的值 int 以分隔 属性 数组,有什么简单的方法可以做到这一点。下划线和 angularjs 实用程序都可以。

例如,我有一个对象数组,

 $scope.expNumArray = [];
 $scope.itemHrArray = []; 
 $scope.highReArray = [];

$scope.rowdata = [{
    "expNum": "678",    
    "itemHr": "",   
    "highRe": "C"
    }, {
    "expNum": "978",    
    "itemHr": "3",  
    "highRe": ""
}];

为此我需要具备以下条件:

 $scope.expNumArray = ["678", "978"];

 $scope.itemHrArray = ["", "3"];

 $scope.highReArray = ["C",""];

可以使用underscore的each函数来循环

 $scope.rowdata

并附加到其他三个数组中的每一个。比 Javascript for 循环更简洁。 This article 关于利用下划线可能也很有趣。

您可以使用 angular 的 forEach 来实现。

$scope.expNumArray = [];
 $scope.itemHrArray = []; 
 $scope.highReArray = [];



$scope.rowdata = [{
    "expNum": "678",    
    "itemHr": "",   
    "highRe": "C"
    }, {
    "expNum": "978",    
    "itemHr": "3",  
    "highRe": ""
}];

angular.forEach($scope.rowdata,function(value,key){

   $scope.expNumArray.push(value["expNum"]);
   $scope.itemHrArray.push(value["itemHr"]);
   $scope.highReArray.push(value["highRe"]);

});