本文介绍了使用javascript重命名数组中的对象键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的有角度的应用程序中,我得到如下所示的Json数据.
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"}]
我需要类似下面的输出
[{"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"}]
这是我的代码
$rootScope.DashboardData["Name"] = widget.seriesname ;
delete $rootScope.DashboardData[widget.seriesname];
$rootScope.DashboardData["data"] = widget.dataname ;
delete $rootScope.DashboardData[widget.seriesname];
widget.chartSeries = $rootScope.DashboardData;
widget.seriesname是"id",widget.dataname是"name".
where widget.seriesname is "id" and widget.dataname is "name".
问题:密钥未更改!
推荐答案
使用map
函数:
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]};
});
这篇关于使用javascript重命名数组中的对象键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!