Javascript 更改对象数组中的键名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6809659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:17:47  来源:igfitidea点击:

Changing the key name in an array of objects?

javascript

提问by John Cooper

How can I change the key name in an array of objects?

如何更改对象数组中的键名?

var arrayObj = [{key1:'value1', key2:'value2'},{key1:'value1', key2:'value2'}];

How can I change each key1to strokeso that I get:

我怎样才能改变每一个key1stroke以便我得到:

var arrayObj = [{stroke:'value1', key2:'value2'},{stroke:'value1', key2:'value2'}];

回答by Paul

var i;
for(i = 0; i < arrayObj.length; i++){
    arrayObj[i].stroke = arrayObj[i]['key1'];
    delete arrayObj[i].key1;
}

回答by Marcus

In recent JavaScript (and TypeScript), use destructuring with rest pattern, spread, and array mapto replace one of the key strings in an array of objects.

在最近的 JavaScript(和 TypeScript)中,使用带有 rest patternspreadarray map 的解构来替换对象数组中的一个关键字符串。

const arrayOfObj = [{key1:'value1', key2:'value2'},{key1:'value1', key2:'value2'}];
const newArrayOfObj = arrayOfObj.map(({ key1: stroke, ...rest }) => ({ stroke, ...rest }));
// [{stroke:'value1', key2:'value2'},{stroke:'value1', key2:'value2'}]

回答by Guffa

You can't change a property name, you have to add the value with a new name and delete the old property:

您无法更改属性名称,必须使用新名称添加值并删除旧属性:

for (var i = 0; i < arrayObj.length; i++) {
  arrayObj[i].stroke = arrayObj[i].key1;
  delete arrayObj[i].key1;
}

回答by alex351

ES6 map()method:

ES6 map()方法:

var arrayObj = [{key1:'value1', key2:'value2'},{key1:'value1', key2:'value2'}];

arrayObj = arrayObj.map(item => {
      return {
        value: item.key1,
        key2: item.key2
      };
    });

回答by jayesh sheta

just one line of code needed with ES6

ES6 只需要一行代码

try following code :

尝试以下代码:

arrayObj.map(({ stroke, key2 }) => ({ yourNewKey: stroke, yourNewKey2: key2 }));

回答by jfriend00

You don't change a key name. You can assign a new key name/value and then remove the previous key if you want. In your example:

您无需更改密钥名称。您可以分配一个新的键名/值,然后根据需要删除以前的键。在你的例子中:

var arrayObj = [{key1,'value1', key2:'value2'},{key1,'value1', key2:'value2'}];
var o = arrayObj[0];   // get first object
var val = o.key1;      // get current value
o.stroke = val;        // assign to new key
delete o.key1;         // remove previous key

If you wanted to do that for all the objects in your main array, you would just put that in a loop that iterates over the contents of your array. I've put more intermediate assignments in here than neccessary just to document what's going on.

如果您想对主数组中的所有对象执行此操作,只需将其放入循环遍历数组内容即可。我在这里放置了更多的中间作业,而不是仅仅为了记录正在发生的事情。

Or a shortened version in a loop:

或者循环中的缩短版本:

for (var i = 0; i < arrayObj.length; i++) {
    var o = arrayObj[i];
    o.stroke = o.key1;
    delete o.key1;
}

回答by Pablo Fernandez

function changeKey(originalKey, newKey, arr)
{
  var newArr = [];
  for(var i = 0; i < arr.length; i++)
  {
    var obj = arr[i];
    obj[newKey] = obj[originalKey];
    delete(obj[originalKey]);
    newArr.push(obj);
  }
  return newArr;
}