0

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

3
  • why would you actually wants to do like this, having object is easier to access then the array of clusters. Commented Jun 23, 2014 at 2:28
  • I have code that goes through each array and puts in the csv file, making these objects in arrays would just be easier for me. Commented Jun 23, 2014 at 2:34
  • 3
    You can't, an array can only have numerical indices, not key / value pairs, as then it would be an object. Commented Jun 23, 2014 at 2:42

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

7 Comments

do I replace data with $scope.exportData?
I replaced data.length and (data[i]) with $scope.exportData and replaced data[i] = with newArray and its pretty much working, it just each value returns ""Name":"Steve"" how would I get it to only have "Steve" as the value?
There are 53 items Name, Age and Sex were just an example. I tried to do another split for : but got an error :( undefined is not a function :(
Try now... i thought only three fields are there. now i have changed for n fields
@Bharath why you use arr.slice()? Should be like: jsbin.com/yimif/1/edit
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.