To rename a key in an object:Use bracket notation to assign the value of the old key to the new key. Use the delete operator to delete the old key. The object will contain only the key with the new name.
Syntax: obj['New key'] = obj['old key']; Note: Renaming the object by simple assignment of variable could be applied on multiple key, value pairs.
I am checking the attributes in a JavaScript object, replacing some of the keys by deleting the prefix "element" and saving the new values in another object.
var keys = Object.keys(json);
for (var j=0; j < keys.length; j++) {
key = keys[j].replace("element_", "");
switch(key) {
default :
tmp[key] = json[key];
break;
}
}
The matter is that when I do that, I can log all the keys, they have the correct names, but when I try to set the values associated to these keys, they are undefined (json[key]).
Is that due to the fact that I converted the keys (Objects
) to Strings
(with the replace
method)?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With