What's the best way (most efficient) to turn all the keys of an object to lower case?

前端 未结 20 2483
野性不改
野性不改 2020-12-04 20:42

I\'ve come up with

function keysToLowerCase (obj) {
  var keys = Object.keys(obj);
  var n = keys.length;
  while (n--) {
    var key = keys[n]; // \"cache\"         


        
相关标签:
20条回答
  • 2020-12-04 21:36

    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}
    
    0 讨论(0)
  • 2020-12-04 21:37

    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)
      })
    })
    
    0 讨论(0)
  • 2020-12-04 21:38

    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]) 
                    
                 }
               }
              
            })
    
    0 讨论(0)
  • 2020-12-04 21:39

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

    • Object.keys()
    • Array. prototype.reduce()

    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') ? objectKeysToLowerCase(val) : val;
            newObj[key.toLowerCase()] = newVal;
            return newObj;
        }, {});
    };
    
    0 讨论(0)
  • 2020-12-04 21:40

    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() + '":'
       }))
    }
    
    0 讨论(0)
  • 2020-12-04 21:42

    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.

    0 讨论(0)
提交回复
热议问题