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

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

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

    var lowerObj = _.transform(obj, function (result, val, key) {
        result[key.toLowerCase()] = val;
    });
    

提交回复
热议问题