Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
81
rated 0 times [  82] [ 1]  / answers: 1 / hits: 24662  / 9 Years ago, thu, march 12, 2015, 12:00:00

In my angular application i am getting an Json Data like below.



[{id:5,name:Immidiate},
{id:4,name:30 days},
{id:3,name:21 days},
{id:2,name:14 days},
{id:1,name:7 days},
{id:6,name:Custom}]


I need an output like below,



[{Name:5,Data:Immidiate},
{Name:4,Data:30 days},
{Name:3,Data:21 days},
{Name:2,Data:14 days},
{Name:1,Data:7 days},
{Name:6,Data:Custom}]


Here is my code



$rootScope.DashboardData[Name] =  widget.seriesname ;
delete $rootScope.DashboardData[widget.seriesname];
$rootScope.DashboardData[data] = widget.dataname ;
delete $rootScope.DashboardData[widget.seriesname];
widget.chartSeries = $rootScope.DashboardData;


where widget.seriesname is id and widget.dataname is name.



Problem: Key is not changed!


More From » json

 Answers
7

Use the map function:



var array = [{id:5,name:Immidiate},
{id:4,name:30 days},
{id:3,name:21 days},
{id:2,name:14 days},
{id:1,name:7 days},
{id:6,name:Custom}];

var resultArray = array.map(function(elm) {
return { Name: elm[widget.seriesname], Data: elm[widget.dataname]};
});

[#67462] Wednesday, March 11, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazminuniquer

Total Points: 63
Total Questions: 121
Total Answers: 96

Location: Cambodia
Member since Thu, May 21, 2020
4 Years ago
;