I have an object. Is there a way to run toUppercase on all of its keys? What I'm doing is trying to uppercase every element in this object
JSON.stringify(JSONObj.people).toUpperCase()
I haven't gotten the above command to work for me. I'm a bit new to this, so appreciate any help!
Object.withUpperCaseKeys = function upperCaseKeys(o) {
// this solution ignores inherited properties
var r = {};
for (var p in o)
r[p.toUpperCase()] = o[p];
return r;
}
Use this method to create a new object with different keys:
JSONObj.people = Object.withUpperCaseKeys(JSONObj.people);
If you want to change an object (modify the instance), use
Object.upperCaseKeys = function upperCaseKeys(o) {
// this solution ignores inherited properties
for (var p in o)
if (p.toUpperCase() != p) {
o[p.toUpperCase()] = o[p];
delete o[p];
}
return o; // just for easier chaining
}
Object.upperCaseKeys(JSONObj.people);
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