Lee Lee
Lee Lee

Reputation: 583

Convert Objects to array in angularjs

I have this data:

 $scope.data = [
        {
            data1:"1",
            data2:"2"

        },
        {
            data1:"1",
            data2:"2"
        }
      ];

I want to output them in console log as:

[ [1,2], [1,2] ]

Any suggestions please.

Upvotes: 0

Views: 53

Answers (2)

Noah Freitas
Noah Freitas

Reputation: 17430

$scope.data = [{
    data1:"1",
    data2:"2"
}, {
    data1:"1",
    data2:"2"
}].map(function (o) {
    return [o.data1, o.data2];

    // or, if you really need the items to be numbers:

    // return [o.data1, o.data2].map(Number);
});

Upvotes: 1

Jeffrey A. Gochin
Jeffrey A. Gochin

Reputation: 964

Yao,

Look at Lodash or ngLodash. Lodash has lots of function that do exactly what you are looking to do. ngLodash is a fork and AngularJS rewrite of Lodash.

https://github.com/rockabox/ng-lodash

https://lodash.com

Hope this helps!

Regards, Jeff

Upvotes: 0

Related Questions