When using $http, I received the following JSON response:
{
"fruits": [
{
"name": "apple",
"prices": [
{
"2015": 2
},
{
"2014": 3
},
{
"2013": 5
}
]
},
{
"name": "banana",
"prices": [
{
"2015": 1
},
{
"2014": 3
},
{
"2013": 4
}
]
}
]
}
In Javascript, I am attempting to create a mapping function to obtain two specific sets of data:
$scope.new_data = [
{
name: 'apple',
data: [5,3,2]
},
{
name: 'banana',
data: [4,3,1]
}
]
and
$scope.years = ['2013','2014','2015']
I have considered the following approach, but I'm unsure how to separate the keys and values:
$scope.new_data = $scope.fruits.map(function(fruit){
return {
name: fruit.name,
data: fruit.prices
};
});
$scope.years = $scope.fruits.map(function(fruit){
return [
fruit.prices.reverse();
];
});