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

前端 未结 20 2590
野性不改
野性不改 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:21

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

提交回复
热议问题