0

I have a nested object with nested keys that I would like to make all lowercase:

let input : {
   "KEY1" : "VALUE1",
   "KEY2" : {"SUBKEY1":"SUBVALUE2"}
}

So the resulting value result should be:

console.log(result)
{
   "key1" : "VALUE1",
   "key2" : {"subkey1":"SUBVALUE2"}
}

How to lowercase all the keys in a nested object?

alphanumeric
  • 17,967
  • 64
  • 244
  • 392
  • 1
    What have you tried, and what exactly is the problem with it? – jonrsharpe Jul 18 '22 at 14:52
  • 3
    you have `=` instead of `:` in your object initialization... no strictly related to the question by the way – Diego D Jul 18 '22 at 14:54
  • https://stackoverflow.com/questions/12539574/whats-the-best-way-most-efficient-to-turn-all-the-keys-of-an-object-to-lower – traynor Jul 18 '22 at 15:06
  • @traynor: the suggested dup is not recursive. – Louys Patrice Bessette Jul 18 '22 at 15:09
  • 2
    @LouysPatriceBessette https://stackoverflow.com/a/41072596/4321299 – traynor Jul 18 '22 at 15:10
  • I would personally decouple the `toLowerCase()` functionality and create a key mapper that can easily achieve this. `rMapKeys(key => key.toLowerCase(), input)` Decoupling the recursive key mapping and the transformation itself yields a lot more flexibility. Here is an example of how to implement `rMapKeys` https://gist.github.com/3limin4t0r/fa8be85a063ab351d19bc602f6b4a77b – 3limin4t0r Jul 18 '22 at 17:48

3 Answers3

5

I would use recursion here.

const input = {
   "KEY1": "VALUE1",
   "KEY2": {"SUBKEY1": "SUBVALUE2"},
   "KEY3": {"SUBKEY2": {"SUBSUBKEY1": "HELLO WORLD!"}},
   "KEY4": null
};

function isPlainObject(input) {
   return input && !Array.isArray(input) && typeof input === 'object';
}

function propertyNamesToLowercase(obj) {
  const final = {};
  
  // Iterate over key-value pairs of the root object 'obj'
  for (const [key, value] of Object.entries(obj)) {
    // Set the lowercased key in the 'final' object and use the original value if it's not an object
    // else use the value returned by this function (recursive call).
    final[key.toLowerCase()] = isPlainObject(value) ? propertyNamesToLowercase(value) : value;
  }
  return final;
}

console.log(propertyNamesToLowercase(input));
Vid
  • 440
  • 4
  • 10
  • 1
    This will throw if any of the values are `null` and will turn an array in to an object. – lusc Jul 18 '22 at 14:58
  • @lusc think I fixed it in the edit, thanks for pointing it out – Vid Jul 18 '22 at 15:00
  • I assume you might want to traverse arrays as wall in case a value is an array, although it's an edge case for the current question. – briosheje Jul 18 '22 at 15:05
  • 2
    A cleaner way to do the for-loop is to have a better base case. If you place the `if (!isPlainObject(obj)) return obj;` guard clause at the start of the function. You can simplify the for-loop content to `final[key.toLowerCase()] = propertyNamesToLowercase(value);`. Additionally, if you want to support objects in arrays, you can add a simple `if (Array.isArray(obj)) return obj.map(propertyNamesToLowercase);` before the first guard clause. – 3limin4t0r Jul 18 '22 at 15:31
0

This is a recursive function cloning an object while using the lowercase version of its property names.

I tried to make it work also with arrays and it will since those are still objects but I prevent it to list the length property.

I also considered the null value as suggested by a user in comments below.

let input = {
   "KEY1" : "VALUE1",
   "KEY2" : {"SUBKEY1":"SUBVALUE2"},   
   "KEY3" : [
      {
        "KEY4" : 'value',
      },
      2,      
   ],
   "KEY4" : null,
}

const clone = cloneObject(input);
console.log(clone);

function cloneObject(o){
  if (o === null)
    return null;
  const clone = {};
  for(k of Object.getOwnPropertyNames(o)){
    if( Array.isArray(o) && k === 'length')
      continue;
    newPropertyName = k.toLowerCase();
    clone[newPropertyName] = (typeof o[k] === 'object') ? cloneObject(o[k]) : o[k];
    delete o[k];
  }
  return clone;
}
Diego D
  • 6,156
  • 2
  • 17
  • 30
-3

You can iterate through the keys and make a new object

const result = Object.keys(input).map((currentKey) => {
    const newKey = currentKey.toLowerCase(); // generating a new key
    return { newKey: input[currentKey]} // building a new object entry for all the keys
})

console.log(result);