-1

I have an object:

var obj = {
  'a|a' : 1,
  'b|b' : 2
}

I wanted to change the obj to something like:

var obj = {
   'aa' : 1,
   'bb' : 2
}

where the attribute a|a was changed to aa.

Is there a way to do the same ?

Adithya
  • 2,923
  • 5
  • 33
  • 47
  • 2
    Before I can answer this question in a useful fashion, what is your desired use case for this? – dbr Apr 21 '17 at 17:04
  • Possible duplicate of [change property name](http://stackoverflow.com/questions/8483425/change-property-name) – aw04 Apr 21 '17 at 17:06
  • @DeanBrunt : I am populating a kendo grid which doesn't allow spaces or any special characters in the 'field' attribute. My server is actually translating the 'field' and 'title' attribute into a single object attribute i.e. 'a|a'. Ultimately i need to change the attribute to 'aa'. – Adithya Apr 27 '17 at 07:10

3 Answers3

3

You can do it like this:

Object.getOwnPropertyNames(obj).forEach(function (key) {
    obj[key.replace('|', '')] = obj[key];
    delete obj[key];
});

Simply iterate through all object keys and assign values to new key (without | characted), and delete the old key afterwards.

Martin Adámek
  • 16,771
  • 5
  • 45
  • 64
2
        var obj = {
            'a|a': 1,
            'b|b': 2
        }

        let keys = Object.keys(obj);
        let newObj = {};
        for (let key of keys) {
            let transformedKey = key.replace("|","") ; // transform your key
            newObj[transformedKey] = obj[key]
        }

        console.log(newObj);

This is fix your usecase.

Abhilash
  • 196
  • 11
2

There are a number of ways to iterate over an Object. I think the most straightforward method is using a for..in statement:

for (let key in obj) {
   console.log(key, '=>', obj[key]);
}

So changing the key name would involve using String.replace to change the key name:

var obj = {
  'a|a' : 1,
  'b|b' : 2
}

let newObj = {};
for (let key in obj) {
   if (obj.hasOwnProperty(key)) {
      newObj[key.replace('|', '')] = obj[key];
   }
}

console.log(newObj);

If you don't want to create a new object, you could add the new key and delete obj[key]

for (let key in obj) {
   if (obj.hasOwnProperty(key)) {
      obj[key.replace('|', '')] = obj[key];
      delete obj[key];
   }
}

Another method would be to use Array.reduce over the keys/properties:

Object.getOwnPropertyNames(obj).reduce((p, c) => {
  p[c.replace('|', '')] = obj[c];
  return p;
}, {});
Rob M.
  • 35,491
  • 6
  • 51
  • 50
  • can you explain what is the meaning of obj[key.replace('|', '')] = obj[key]; – Adithya Apr 27 '17 at 07:13
  • 1
    Sure, `key` is going to look like `"a|a"`, so `key.replace('|', '')` will yield `'aa'`. The value of `obj['a|a']` (or `obj[key]`) is 1, so this would look like `obj['aa'] = obj['a|a']` or `obj['aa'] = 1` – Rob M. Apr 27 '17 at 20:11
  • obj[key.replace('|', '')] creates a new key within the existing object and '= obj[c]' assigns the value of 'obj[c]' to the new key ? Is this behavior due to immutability of Strings ? Is that the reason we have to delete the old key ? – Adithya Apr 28 '17 at 08:58
  • 1
    Exactly, it is creating a new key and assigning `obj[c]`'s value to it; however, it is not due to the immutability of strings, but the immutability of object properties names, which is why we have to delete the old key. – Rob M. Apr 28 '17 at 16:32