How to uppercase Javascript object keys?
As of 2019 you can use Object.fromEntries
:
let populations = {london: 8.9, beijing: 21.54, mumbai: 18.41}; // March 2020
let entries = Object.entries(populations);
let capsEntries = entries.map((entry) => [entry[0][0].toUpperCase() + entry[0].slice(1), entry[1]]);
let capsPopulations = Object.fromEntries(capsEntries);
console.log(capsPopulations);
Loop through delete and replace:
var obj = [{key1: 1,key2: 1},{key3: 1,key4: 1}];
for(var i = 0; i<obj.length;i++) {
var a = obj[i];
for (var key in a) {
if (a.hasOwnProperty(key)) {
a[key.charAt(0).toUpperCase() + key.substring(1)] = a[key];
delete a[key];
}
}
obj[i] = a;
}