-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 ?

3
  • 2
    Before I can answer this question in a useful fashion, what is your desired use case for this? Commented Apr 21, 2017 at 17:04
  • Possible duplicate of change property name Commented Apr 21, 2017 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'. Commented Apr 27, 2017 at 7:10

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

This is a great solution. I would also suggest Object.getOwnPropertyNames() instead of Object.keys() to ignore enumerability...
True, will update my answer. Thanks for pointing it out, didn't know about this difference.
Well, your solution with Object.keys() was perfectly valid. This was just a suggestion, you did not have to edit your code. ;)
Well I think getOwnPropertyNames is better in this case :]
As you wish... You're the king of your answer! :P
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; }, {}); 

4 Comments

can you explain what is the meaning of obj[key.replace('|', '')] = obj[key];
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
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 ?
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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.