简体   繁体   中英

Javascript convert an array of objects to 1 array

I have this array defined:

$scope.exportData = [];

and then I do an api call and it returns 3 objects in my array.

[Object, Object, Object] //from Console Log

0: Object
  Name: "Steve"
  Age: 30
  Sex: "M"
1: Object
  Name: "James"
  Age: 25
  Sex: "M"
2: Object
  Name: "Joe"
  Age: 20
  Sex: "M"

what I am looking to do here is take the objects inside the $scope.exportData and turn them in arrays so I have 5 arrays inside $scope.exportData like so:

[Array[3], Array[3], Array[3]]
   0: Array[3]
      "Name" : "Steve"
      "Age" : 30
      "Sex" : "M"
   1: Array[3]
      "Name" : "James"
      "Age" : 25
      "Sex" : "M"
   2: Array[3]
      "Name" : "Joe"
      "Age" : 20
      "Sex" : "M"

how would I do this? Thanks, any help would be appreciated

They try like this, if data is the array of objects, then

for(var i = 0; i < $scope.exportData.length; i++) {
    var arr = [];
    for(j in $scope.exportData[i]) {
        arr.push($scope.exportData[i][j]);
    }
    $scope.exportData[i] = arr;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM