Lee Lee
Lee Lee

Reputation: 583

Angularjs: Convert objects to arrays

This is my data:

 $scope.data = [ 
           {
            "top":"2",
            "status":"1",
            "name":"Anna",
           },
           {
            "top":"2",
            "status":"1",
            "name":"Jodie",
           },
           {
            "top":"2",
            "status":"1",
            "name":"susan",
           }
];

Now I want to convert the objects to arrays. When I run my program I get this following output in console log:

{
  "1":{
       "2":"3"
      }
}

But I need them to output as an array not object. Someone told me to use map function but I need to do them without using map function because it will cause a conflict in the process of my project. Is there any other way to do it without using map function?

The 1 in output means status object. 2 means top object and 3 means the number of students who's top is in 2.

This is the code I have so far: http://jsfiddle.net/DharkRoses/sjz5aLv1/

Any suggestions? TIA

Update: I want the output which looks like this:

(
  [1] =>Array
       (
       [2] => 3
       )
)

Upvotes: 0

Views: 98

Answers (1)

okanozeren
okanozeren

Reputation: 555

I took below array for processing:

$scope.data = [
    { "top": "1", "status": "1", "name": "Anna" },
    { "top": "2", "status": "1", "name": "Jodie" },
    { "top": "2", "status": "1", "name": "Susan" },
    { "top": "3", "status": "1", "name": "Eddie" },
    { "top": "2", "status": "2", "name": "Megan" },
    { "top": "2", "status": "2", "name": "Frank" }
];

If I am not wrong you want a response like that:

[
    { 
        "1": [
                { "1": 1 },
                { "2": 2 },
                { "3": 1 }
        ]
    },
    {
        "2": [
                { "2": 2 }
        ]
    }
]

This is a live demo in order to achieve above result: http://jsfiddle.net/nerezo/vz0dbdeq/

Upvotes: 2

Related Questions