使用javascript重命名数组中的对象键

IT技术 javascript json angularjs html
2021-03-11 05:31:29

在我的 angular 应用程序中,我得到了如下所示的 Json 数据。

[{"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”。

问题:密钥没有改变!

6个回答

使用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]};
});
现在我在一个变量中有 id 和 name。那么如何在不对 id 和 name 进行硬编码的情况下获得该值?
2021-04-24 05:31:29
新键位于您在地图函数内返回的对象中(“名称”、“数据”)
2021-04-25 05:31:29
所以我应该一个一个元素传入这个方法?
2021-04-30 05:31:29
没有它的整个阵列。看看我的编辑,你的输出是 resultArray 变量
2021-05-15 05:31:29
const arr = [{"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"}];

const resultArray = arr.map(elm => ({ Name: elm.id, Data: elm.name}));

console.log(resultArray);

编辑:刚刚看到你的 angular 标签 - 使用 angular 的 forEach:

var out = [];

angular.forEach(data, function (obj) {
    out.push({
        Name: obj.id,
        Data: obj.name
    });
});

无角度:

对于现代浏览器,使用 array.map:

var out = data.map(function (obj) {
   return {
        Name: obj.id,
        Data: obj.name
    };
});

console.log(out);

在旧浏览器中:

var data = [{"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 out = [];

for (var key in data) {
    if (data.hasOwnProperty(key)) {
        out.push({
            'Name': data[key].id,
            'Data': data[key].name
        });
    }
}

console.log(out);

你也可以这样做:

arr.map(({id, name}) =>({name: id, data:name}))

const objArr = [
    { 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" },
  ];

objArr.map(({id, name}) =>({name: id, data:name}))
事实上,我在这里写了一些关于这个的一些问题:coderanx.com/javascript/...
2021-05-02 05:31:29
有趣的。由于该map函数没有用 明确描述语法{id, name},我不得不深入挖掘一下:这是一个map称为“对象解构”的通用 javascript 功能(不仅仅是用于
2021-05-10 05:31:29

您还可以使用Underscopre.js map方法:

$scope.newList = _.map(list, function(item) {
 return { Name: item.id, Data: item.name};
});

参见示例 Fiddle