98

I've come up with

function keysToLowerCase (obj) {
  var keys = Object.keys(obj);
  var n = keys.length;
  while (n--) {
    var key = keys[n]; // "cache" it, for less lookups to the array
    if (key !== key.toLowerCase()) { // might already be in its lower case version
        obj[key.toLowerCase()] = obj[key] // swap the value to a new lower case key
        delete obj[key] // delete the old key
    }
  }
  return (obj);
}

But I'm not sure how will v8 behave with that, for instance, will it really delete the other keys or will it only delete references and the garbage collector will bite me later ?

Also, I created these tests, I'm hoping you could add your answer there so we could see how they match up.

EDIT 1: Apparently, according to the tests, it's faster if we don't check if the key is already in lower case, but being faster aside, will it create more clutter by ignoring this and just creating new lower case keys ? Will the garbage collector be happy with this ?

João Pinto Jerónimo
  • 9,586
  • 15
  • 62
  • 86
  • 2
    Can you create a new object rather than modifying the existing one? You would get to skip all the deletes. – Jason Orendorff Sep 22 '12 at 00:32
  • I think your code is reasonable as is. _"it's faster if we don't check if the key is already in lower case, but ... will it create more clutter by ignoring this and just creating new lower case keys"_ - that code doesn't actually work (it will end up deleting any keys that were already lower case), so it really doesn't matter how fast it is... – nnnnnn Sep 22 '12 at 00:33
  • @JasonOrendorff apparently its slower, and it would create one unneeded object every time, and the garbage collector wouldn't be happy with that... – João Pinto Jerónimo Sep 22 '12 at 00:40
  • @JoãoPintoJerónimo What you're seeing in your speed tests is that the code is running thousands of times on the same object. Of course after the test runs once, there's no more work to do; all the keys are lowercase already. If you test it by creating lots of objects with lots of keys: http://jsperf.com/object-keys-to-lower-case/7 then all three implementations are dramatically slower, but creating a new object is slightly faster in both Firefox and Chrome. – Jason Orendorff Sep 22 '12 at 03:28
  • Oh I see @JasonOrendorff... I'll make another revision of the tests – João Pinto Jerónimo Sep 22 '12 at 03:34
  • Unfortunately your tests is wrong. The SETUP part is only run ONCE for every clocked loop. Since you declared the object in the setup, it is only the first iteration of the first test that actually changes the object keys to lower case, all other iteration get an object where the keys already is in lower case. Fixed in version [10](http://jsperf.com/object-keys-to-lower-case/10). (I made a similar comment about an hour ago, where I said it was only run once. I noticed that that was wrong, so I removed my comment and did some more testing) – some Sep 22 '12 at 03:40
  • @some I think JasonOrendorff corrected it in version 7, I added one more function that doesn't cache the key and everything comes neck to neck (for me, [1014, 1080, 1008]): http://jsperf.com/object-keys-to-lower-case/11 – João Pinto Jerónimo Sep 22 '12 at 03:53
  • @JoãoPintoJerónimo Yeah, too bad that I deleted my comment. However, look at the code at revision 10. There is uses your original names of the keys (both short and long) and has a function to verify that it actually returns the expected input. I also added some new tests, where at least one is the fastest in every browser I tested. – some Sep 22 '12 at 04:28
  • @JoãoPintoJerónimo The `key.toLowerCase()` is called two times, you can store it in a variable and use it in next line. And it is strange that creating a new object and just adding new keys to it is slower than: adding new keys to original object + deleting previous keys? – S.Serpooshan Aug 29 '22 at 17:24

24 Answers24

85

The fastest I come up with is if you create a new object:

var key, keys = Object.keys(obj);
var n = keys.length;
var newobj={}
while (n--) {
  key = keys[n];
  newobj[key.toLowerCase()] = obj[key];
}

I'm not familiar enough with the current inner working of v8 to give you a definitive answer. A few years ago I saw a video where the developers talked about objects, and IIRC it will only delete the references and let the garbage collector take care of it. But it was years ago so even if it was like that then, it doesn't need to be like that now.

Will it bite you later? It depends on what you are doing, but probably not. It is very common to create short lived objects so the code is optimized to handle it. But every environment has its limitations, and maybe it will bite you. You have to test with actual data.

some
  • 48,070
  • 14
  • 77
  • 93
  • 1
    This is the best answer, but @JasonOrendorff also deserves credit for this. – João Pinto Jerónimo Sep 22 '12 at 22:28
  • I think this needs a `var` in front of `key = keys[n]` ? – Andrew Ferrier Jun 06 '13 at 15:52
  • @AndrewFerrier Correct. Fixed. – some Jul 23 '13 at 21:27
  • This only does one layer if you have something like {A:1, B : { C: 2 }} A and B would changed but not C – Michael Warner Jan 17 '17 at 21:31
  • 1
    @MichaelWarner That is correct, it only touches the keys in the object that you give to it and do not touch the children. That was what the question was about. It looks like you want to do a deep copy, but that is a dark deep rabbit hole... – some Jan 18 '17 at 03:20
  • omg, why on EARTH would you use a sentinel loop like this in Javascript????... you could use ANY approach and deal with less "omg my entire business is under fire" situations in PROD. Like, dear God... sentinel loops in a non-strongly-typed language... man. Ewww... The forEach is there for a reason... – GoreDefex May 05 '19 at 16:06
  • @GoreDefex In 2012 `forEach` wasn't available in most used browsers. Also, the question was for "the most efficient" way to do it, and `forEach` was slower. – some May 05 '19 at 17:04
  • @some The user did not state their definition of "more efficient". To me, a professional in the industry, cleaner, easier-to-read code is more efficient in that it produces less bugs and therefore less development hours. Also, in 2012 you'd have to transpile to use a forEach loop (which we all did), therefore it was actually a pre-determined for loop by run-time. – GoreDefex Apr 02 '20 at 17:13
  • @GoreDefex Well, if you look at what the user (not you) wrote in their update (" it's faster if") and the fact that the user (again, not you) accepted this answer, it it very likely that the user wanted the code that was the fastest in the v8 engine 8 years ago. Write your own answer if this is so terrible. What traspiler did you use in 2012? – some Apr 03 '20 at 05:17
  • @some "it's faster if" is not a qualifying sentence to define "most efficient". Playing detective with the users intention is not what SO is for. Giving answers until one is accepted, is. Also, guessing what the user wanted 8 years ago is a severe waste of time due to several moot points. Lastly, please explain in detail how in efficient a forEach loop is compared to a sentinel for loop... When transpiled. – GoreDefex Apr 03 '20 at 07:53
  • 1
    A [`for...in`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in) loop is probably faster and more readable. `for (const key in obj) newObj[key.toLowerCase()] = obj[key];` – 3limin4t0r Jul 18 '22 at 15:20
  • @3limin4t0r Have you made any profiling? – some Oct 04 '22 at 12:07
55

Using Object.fromEntries (ES10)

Native and immutable solution using the new Object.fromEntries method:


const newObj = Object.fromEntries(
  Object.entries(obj).map(([k, v]) => [k.toLowerCase(), v])
);

Until that function becomes widely available you could define it yourself with the following polyfill:

Object.fromEntries = arr => Object.assign({}, ...Array.from(arr, ([k, v]) => ({[k]: v}) ));

A nice thing is that this method does the opposite of Object.entries, so now you can go back and forth between the object and array representation.

Yves M.
  • 29,855
  • 23
  • 108
  • 144
40

I'd use Lo-Dash.transform like this:

var lowerObj = _.transform(obj, function (result, val, key) {
    result[key.toLowerCase()] = val;
});
caleb
  • 2,687
  • 30
  • 25
  • 3
    You can now use [`Object.fromEntries`](https://stackoverflow.com/a/54985484/1480391) to achieve the same kind of transformation, but natively, with vanilla JavaScript – Yves M. Mar 10 '19 at 11:45
  • @YvesM. [not supported by IE11](https://caniuse.com/#search=fromEntries) :( – Mosh Feu Apr 16 '20 at 07:22
40

Personally, I'd use:

let objectKeysToLowerCase = function (origObj) {
    return Object.keys(origObj).reduce(function (newObj, key) {
        let val = origObj[key];
        let newVal = (typeof val === 'object') ? objectKeysToLowerCase(val) : val;
        newObj[key.toLowerCase()] = newVal;
        return newObj;
    }, {});
}

It's succinct, recurs to handle nested objects and returns a new object rather than modifying the original.

In my limited local testing this function is faster than the other recursive solution currently listed (once fixed). I'd love to benchmark it against the others but jsperf is down at the moment (???).

It's also written in ES5.1 so, according to the docs on MDN, should work in FF 4+, Chrome 5+, IE 9.0+, Opera 12+, Safari 5+ (so, pretty much everything).

Vanilla JS for the win.

I wouldn't worry too much about the garbage collection aspect of all this. Once all references to the old object are destroyed it will be GC's but the new object will still reference basically all it's properties, so they will not.

Any Functions, Arrays or RegExp will be "copied" across by reference. In terms of memory, even Strings will not be duplicated by this process since most (all?) modern JS engines user string interning. I think that leaves just the Numbers, Booleans and the Objects that formed the original structure left to be GC'd.

Note that (all implementations of) this process will lose values if the original has multiple properties with the same lowercase representation. Ie:

let myObj = { xx: 'There', xX: 'can be', Xx: 'only', XX: 'one!' };
console.log(myObj);
// { xx: 'There', xX: 'can be', Xx: 'only', XX: 'one!' }

let newObj = objectKeysToLowerCase(myObj);
console.log(newObj);
// { xx: 'one!' }

Of course, sometimes this is exactly what you want.

Update 2018-07-17

A few people have noted the original function doesn't work well with arrays. Here's an expanded, more resilient version. It recurs correctly through arrays and works if the initial value is an array or simple value:

let objectKeysToLowerCase = function (input) {
    if (typeof input !== 'object') return input;
    if (Array.isArray(input)) return input.map(objectKeysToLowerCase);
    return Object.keys(input).reduce(function (newObj, key) {
        let val = input[key];
        let newVal = (typeof val === 'object') && val !== null ? objectKeysToLowerCase(val) : val;
        newObj[key.toLowerCase()] = newVal;
        return newObj;
    }, {});
};
Thanh Thuy
  • 61
  • 1
  • 5
Molomby
  • 5,859
  • 2
  • 34
  • 27
  • 1
    I suggest this solution as it i a pure function and doesn't mutate the original object – Eugene Fidelin Oct 26 '17 at 15:14
  • 1
    this wont work when value of a key is an array of values.type of would give array as object.@Molomby – VivekN Mar 06 '18 at 18:22
  • 1
    I like your solution but it doesn't work if the value is an array. Suggest modify your condition to check if the value is an Array type or non-object type. e.g `let newVal = ( typeof val !== 'object') || ( Array.isArray( val) )? val: objectKeysToLowerCase(val); ` – Andy Lai May 12 '18 at 03:48
  • 1
    This solution's ability to handle nested objects should be emphasized, nice solution – Kubie Aug 23 '19 at 19:59
  • 2
    I'm using your solution, but it's not working with properties with values = null. I've modified `if (typeof input !== 'object') return input;` to `if (typeof input !== 'object' || input===null) return input;` – Sven Tore Feb 12 '20 at 13:54
  • 1
    To reiterate what's been mentioned above, this will throw errors if any object properties are `null` (the `typeof` check properly accounts for `undefined`). Add `|| input == null` to the first line check to account for this. – zcoop98 Jul 13 '21 at 20:19
12

ES6 version:

Object.keys(source)
  .reduce((destination, key) => {
    destination[key.toLowerCase()] = source[key];
    return destination;
  }, {});
Tom Roggero
  • 5,777
  • 1
  • 32
  • 39
8

The loDash/fp way, quite nice as its essentially a one liner

import {
mapKeys
} from 'lodash/fp'

export function lowerCaseObjectKeys (value) {
return mapKeys(k => k.toLowerCase(), value)
}
Damian Green
  • 6,895
  • 2
  • 31
  • 43
7

Using forEach seems to be a bit quicker in my tests- and the original reference is gone, so deleting the new one will put it in reach of the g.c.

function keysToLowerCase(obj){
    Object.keys(obj).forEach(function (key) {
        var k = key.toLowerCase();

        if (k !== key) {
            obj[k] = obj[key];
            delete obj[key];
        }
    });
    return (obj);
}

var O={ONE:1,two:2,tHree:3,FOUR:4,Five:5,SIX:{a:1,b:2,c:3,D:4,E:5}}; keysToLowerCase(O);

/* returned value: (Object) */

{
    five:5,
    four:4,
    one:1,
    six:{
        a:1,
        b:2,
        c:3,
        D:4,
        E:5
    },
    three:3,
    two:2
}
Szymon Toda
  • 4,454
  • 11
  • 43
  • 62
kennebec
  • 102,654
  • 32
  • 106
  • 127
3

Simplified Answer

For simple situations, you can use the following example to convert all keys to lower case:

Object.keys(example).forEach(key => {
  const value = example[key];
  delete example[key];
  example[key.toLowerCase()] = value;
});

You can convert all of the keys to upper case using toUpperCase() instead of toLowerCase():

Object.keys(example).forEach(key => {
  const value = example[key];
  delete example[key];
  example[key.toUpperCase()] = value;
});
Community
  • 1
  • 1
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
3

Here is easiest solution to convert all the json keys to lower case.

let o = {"Account_Number   ":"0102301", "customer_NaME":"name"}

o = Object.keys(o).reduce((c, k) => (c[k.toLowerCase().trim()] = o[k], c), {})

console.log(o)
Rohit Parte
  • 3,365
  • 26
  • 26
3

With TypeScript

/**
 * Lowercase the keys of an object
 * @example
  lowercaseKeys({FOO: true, bAr: false}); // {foo: true, bar: false}
 */
export function lowercaseKeys<T>(object: { [key: string]: T }): { [key: string]: T } {
  const result: { [key: string]: T } = {};

  for (const [key, value] of Object.entries(object)) {
    result[key.toLowerCase()] = value;
  }

  return result;
}

Usage

lowercaseKeys({FOO: true, bAr: false}); // {foo: true, bar: false}
Zuhair Taha
  • 2,808
  • 2
  • 35
  • 33
3

While the ES10 Object.fromentries() method works

const newObj = Object.fromEntries(
  Object.entries(obj).map(([k, v]) => [k.toLowerCase(), v])
);

You can similarly use the snippet below for ES2015 and below

this.htmlWorkbookJSON = jsonData.map((element: Object) => {
    let entriesArray = Object.entries(element)
    const data = new Object()
    entriesArray.forEach(([key, value]) => {
      data[key.toLocaleLowerCase()] = value;
    })
    return data
  })
desina
  • 31
  • 1
2

This is not the cleanest way but it has worked for my team so it is worth sharing.

I created this method as our backend is running a language that is not case sensitive and the database and backend will produce different key cases. For us, it has worked flawlessly. Mind you we send dates as Strings and we don't send functions.

We have reduced it to this one line.

const toLowerCase = (data) => JSON.parse(JSON.stringify(data).replace(/"([^"]+)":/g, ($0, key) => '"' + key.toString().toLowerCase() + '":'))

We clone the object by using the JSON.parse(JSON.stringify(obj)) method. This produces a string version of the object in the JSON format. While the object is in the string form you can use regex as JSON is a predictable format to convert all keys.

Broken up it looks like this.

const toLowerCase = function (data) {
  return JSON.parse(JSON.stringify(data)
   .replace(/"([^"]+)":/g, ($0, key) => {
     return '"' + key.toString().toLowerCase() + '":'
   }))
}
Michael Warner
  • 3,879
  • 3
  • 21
  • 45
2

I used ES6 and TypeScript. toLowerCaseObject function takes an Array as parameter and looking through Object tree recursively and make every node lowercase:

function toLowerCaseObject(items: any[]) {
        return items.map(x => {
            let lowerCasedObject = {}
                for (let i in x) {
                    if (x.hasOwnProperty(i)) {
                        lowerCased[i.toLowerCase()] = x[i] instanceof Array ? toLowerCaseObject(x[i]) : x[i];
                    }
            }
            return lowerCasedObject;
        });
    }
El.
  • 1,217
  • 1
  • 13
  • 23
2

One-liner (only for top level keys):

Object.assign(...Object.keys(obj).map(key => ({[key.toLowerCase()]: obj[key]})))

Converts:

{ a: 1, B: 2, C: { Z: 4 } }

To:

{ a: 1, b: 2, c: { Z: 4 } }
giladb
  • 101
  • 1
  • 3
1
const keysToLowerCase = object => {
  return Object.keys(object).reduce((acc, key) => {
    let val = object[key];
    if (typeof val === 'object') {
      val = keysToLowerCase(val);
    }
    acc[key.toLowerCase()] = val;
    return acc;
  }, {});
};

Works for nested object.

Martin
  • 195
  • 1
  • 12
0

Consider lowering case just once, storing it in a lowKey var:

function keysToLowerCase (obj) {
  var keys = Object.keys(obj);
  var n = keys.length;
  var lowKey;
  while (n--) {
    var key = keys[n];
    if (key === (lowKey = key.toLowerCase()))
    continue

    obj[lowKey] = obj[key]
    delete obj[key]

  }
  return (obj);
}
moonwave99
  • 21,957
  • 3
  • 43
  • 64
0

Here's my recursive version based on one of the above examples.

//updated function
var lowerObjKeys = function(obj) {
  Object.keys(obj).forEach(function(key) {
    var k = key.toLowerCase();
    if (k != key) {
      var v = obj[key]
      obj[k] = v;
      delete obj[key];

      if (typeof v == 'object') {
        lowerObjKeys(v);
      }
    }
  });

  return obj;
}

//plumbing
console = {
  _createConsole: function() {
    var pre = document.createElement('pre');
    pre.setAttribute('id', 'console');
    document.body.insertBefore(pre, document.body.firstChild);
    return pre;
  },
  info: function(message) {
    var pre = document.getElementById("console") || console._createConsole();
    pre.textContent += ['>', message, '\n'].join(' ');
  }
};

//test case
console.info(JSON.stringify(lowerObjKeys({
  "StackOverflow": "blah",
  "Test": {
    "LULZ": "MEH"
  }
}), true));

Beware, it doesn't track circular references, so you can end up with an infinite loop resulting in stack overflow.

adamkonrad
  • 6,794
  • 1
  • 34
  • 41
  • There's a bug here; the function only recurs `if (k != key)`, so if you have an nested object stored in a lowercase key it won't be touched. The fix is to move the inner `if (typeof v == 'object') { ... }` clause outside the `if (k != key) { ... }` clause. Performance looks about the same. – Molomby Dec 10 '16 at 03:32
  • Oh, and the `var v = obj[key]` assignment too, which *does* reduce performance.. :/ – Molomby Dec 10 '16 at 04:37
0

For all values:

to_lower_case = function(obj) {
    for (var k in obj){
        if (typeof obj[k] == "object" && obj[k] !== null)
            to_lower_case(obj[k]);
        else if(typeof obj[k] == "string") {
            obj[k] = obj[k].toLowerCase();
        }
    }
    return obj;
}

Same can be used for keys with minor tweaks.

Karan
  • 2,120
  • 15
  • 27
0

This is how I do it. My input can be anything and it recuses through nested objects as well as arrays of objects.

const fixKeys = input => Array.isArray(input)
  ? input.map(fixKeys)
  : typeof input === 'object'
  ? Object.keys(input).reduce((acc, elem) => {
      acc[elem.toLowerCase()] = fixKeys(input[elem])
      return acc
    }, {})
  : input

tested using mocha

const { expect } = require('chai')

const fixKeys = require('../../../src/utils/fixKeys')

describe('utils/fixKeys', () => {
  const original = {
    Some: 'data',
    With: {
      Nested: 'data'
    },
    And: [
      'an',
      'array',
      'of',
      'strings'
    ],
    AsWellAs: [
      { An: 'array of objects' }
    ]
  }

  const expected = {
    some: 'data',
    with: {
      nested: 'data'
    },
    and: [
      'an',
      'array',
      'of',
      'strings'
    ],
    aswellas: [{ an: 'array of objects' }]
  }

  let result

  before(() => {
    result = fixKeys(original)
  })

  it('left the original untouched', () => {
    expect(original).not.to.deep.equal(expected)
  })

  it('fixed the keys', () => {
    expect(result).to.deep.equal(expected)
  })
})
Dave Sag
  • 13,266
  • 14
  • 86
  • 134
0
var aa = {ID:1,NAME:'Guvaliour'};
var bb= {};
var cc = Object.keys(aa);
cc.forEach(element=>{
 bb[element.toLowerCase()]=aa[element];
});
cosole.log(bb)
Guvaliour
  • 397
  • 1
  • 5
  • 17
0

The below code to convert the all key in lower case

array.forEach(item=>{
         
          let data = Object.keys(item).reduce((result, p) => (result[p.toLowerCase().trim()] = item[p], result), {})
          
          if(data.hasOwnProperty(fieldname)){
              if(data[fieldname]){
                if(!response['data'].includes(data[fieldname].toLowerCase()))
                    response['data'].push(data[fieldname]) 
                
             }
           }
          
        })
Sumant Singh
  • 904
  • 1
  • 14
  • 16
0

const objectToLowercase = (data) => {
      const values = Object.values(data);
      if (values.length === 0) {
        return data;
      }

      return Object.keys(data).reduce((toLowerKeyObj, key) => {
        const isObject = typeof data[key] === 'object';
        const isArray = Array.isArray(data[key]);
        let value = null;

        if (isObject) {
          if (!isArray) {
            value = objectToLowercase(data[key]);
          }
        }

        if (isArray) {
          value = data[key].map(_value => {
            return objectToLowercase(_value);
          });
        }

        toLowerKeyObj[key.toLowerCase()] = isObject ? value : data[key];
        return toLowerKeyObj;
      }, {});
    };
Recep Özen
  • 1
  • 2
  • 1
0
const newObj = {};

for(const key in obj){
    newObj[key.toLowerCase()] = obj[key];
}
0

Most of the above answers do not handle null and undefined values. To get around it why not use the transform helper function from lodash?

const query = {
  Company: 'GH Works',
  Items: {
    Construction: 'FB',
    LineItems: {
      Quantity: '100',
      QUALity: 'checked'
    }
  }
}


function deepLowercaseKeys(hash) {
  return _.transform(hash, function(result, value, key) {
    const valueIsObject = typeof value === 'object';
    result[key.toLowerCase()] = valueIsObject ? deepLowercaseKeys(value) : value;
  });
}

console.log(deepLowercaseKeys(query))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

Additionally, you can customize the function and then use it to transform the object in any way you like.

const query = {
  Company: 'GH Works',
  Items: {
    Construction: 'FB',
    LineItems: {
      Quantity: '100',
      QUALity: 'checked'
    }
  }
}

// Base function
function deepTransform(hash, callback) {
  return _.transform(hash, function(result, value, key) {
    if (typeof value === 'object') {
      return callback(result, deepTransform(value, callback), key)
    }
    
    callback(result, value, key)
  })
}

// Custom function (can be anything)
function appendHello(hash) {
  return deepTransform(hash, function(result, value, key) {
    result[`${key}_hello`.toLowerCase()] = value;
  })
}

console.log(appendHello(query))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
abeidahmed
  • 27
  • 1
  • 5