Changing the key name in an array of objects?

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 key1 to stroke so that I get:

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

Answers:

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

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

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'}]
Answer

ES6 map() method:

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

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

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;
}
Answer

just one line of code needed with ES6

try following code :

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

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;
}
Answer
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;
}
Answer

const company =  [{ id:"1", name:"somename"},{ id:"1", name:"somename"}]

company.map(({ id, name }) => ({ companyId: id, companyName: name }));

// output
//[{ companyId:"1", companyName:"somename"},{ companyId:"1", //companyName:"somename"}]

Answer

Simple way would be to use forEach() function. First copy key1 content into the stroke and then delete they key1

arrayObj.forEach(function (i) {
    i.stroke = job["key1"];
    delete job.key1;
  });

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.