How to uppercase Javascript object keys?

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;

}

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);

Another approach (more clean)

import * as _ from 'lodash';


function capitalizeObjectKeys(obj) {
  return _.transform(obj, (result, val, key) => {
    result[key.charAt(0).toUpperCase() + key.slice(1)] = val;
  });
}

https://stackblitz.com/edit/js-hsqutg?embed=1&file=index.js


In my case this code worked well. Both in uppercase and lowercase with map:

to uppercase :

const newDataUpperCase = data.map( function( item ){
        for(var key in item){
            var upper = key.toUpperCase();
            if( upper !== key ){ 
              item[ upper ] = item;
                delete item[key];
            }
        }
        return item;
    });

to lowercase :

const newDataUpperCase = data.map( function( item ){
        for(var key in item){
            var lower= key.toLowerCase();
            if( lower!== key ){ 
              item[ lower] = item;
                delete item[key];
            }
        }
        return item;
    });