1

I need to collect each property values in an array of objects int to separate property arrays, is there any simple way to do it. Either underscore and angularjs utilities are fine.

For example, I have an array of objects as,

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

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

Now for this I need to have the following:

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

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

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

2 Answers 2

2

you can use angular's forEach to achieve this.

$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"]);

});
Sign up to request clarification or add additional context in comments.

Comments

0

You can use underscore's each function to loop over

 $scope.rowdata

and append to each of the three other arrays. A bit cleaner than a Javascript for loop. This article about leveraging underscore might be of interest as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.