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

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

    ES6 version:

    Object.keys(source)
      .reduce((destination, key) => {
        destination[key.toLowerCase()] = source[key];
        return destination;
      }, {});
    

提交回复
热议问题