trs
trs

Reputation: 863

How to turn an array of objects into an array of arrays in Angular?

Original array of objects:

$scope.items = [
    {
        'name': 'Apple',
        'value': 10,
        'color': 'green',
        'size': 'medium'
    },
    {
        'name': 'Kiwi',
        'value': 12,
        'color': 'brown',
        'size': 'small'
    },
    {
        'name': 'Lemon',
        'value': 8,
        'color': 'yellow',
        'size': 'small'
    }
];

I want to return:

$scope.filtered_items = [
    ['Apple', 10],
    ['Kiwi', 12],
    ['Lemon', 8]
];

So, two things: first I want to convert an array of objects into an array of arrays and second, I want to only extract 'name' and 'value'.

Upvotes: 0

Views: 67

Answers (1)

hsz
hsz

Reputation: 152206

Just try with:

$scope.filtered_items = $scope.items.map(function(item){
  return [item.name, item.value];
});

Upvotes: 4

Related Questions