可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I've come up with
function keysToLowerCase (obj) {
var keys = Object.keys(obj);
var n = keys.length;
while (n--) {
var key = keys[n]; // "cache" it, for less lookups to the array
if (key !== key.toLowerCase()) { // might already be in its lower case version
obj[key.toLowerCase()] = obj[key] // swap the value to a new lower case key
delete obj[key] // delete the old key
}
}
return (obj);
}
But I'm not sure how will v8 behave with that, for instance, will it really delete the other keys or will it only delete references and the garbage collector will bite me later ?
Also, I created these tests, I'm hoping you could add your answer there so we could see how they match up.
EDIT 1:
Apparently, according to the tests, it's faster if we don't check if the key is already in lower case, but being faster aside, will it create more clutter by ignoring this and just creating new lower case keys ? Will the garbage collector be happy with this ?
回答1:
The fastest I come up with is if you create a new object:
var key, keys = Object.keys(obj);
var n = keys.length;
var newobj={}
while (n--) {
key = keys[n];
newobj[key.toLowerCase()] = obj[key];
}
I'm not familiar enough with the current inner working of v8 to give you a definitive answer. A few years ago I saw a video where the developers talked about objects, and IIRC
it will only delete the references and let the garbage collector take care of it. But it was years ago so even if it was like that then, it doesn't need to be like that now.
Will it bite you later? It depends on what you are doing, but probably not. It is very common to create short lived objects so the code is optimized to handle it. But every environment has its limitations, and maybe it will bite you. You have to test with actual data.
Update 2017:
Added a utility function that can make a shallow or deep copy of an object, supporting circular references. Only briefly tested on node.
/** @summary objectKeysToLowerCase( input, deep, filter )
* returns a new object with all own keys converted to lower case.
* The copy can be shallow (default) or deep.
*
* Circular references is supported during deep copy and the output will have
* the same structure.
*
* By default only objects that have Object as constructor is copied.
* It can be changed with the "filter"-function.
*
* NOTE: If an object has multiple keys that only differs in case,
* only the value of the last seen key is saved. The order is usually
* in the order that the keys where created.
* Exaple : input = {aa:1, aA:2, Aa:3, AA:4}, output = {aa:4};
*
* NOTE: To detect circular references, the list of objects already converted
* is searched for every new object. If you have too many objects, it will
* be slower and slower...
*
* @param {object} input
* The source object
* @param {boolean|number} deep
* A shallow copy is made if "deep" is undefined, null, false or 0.
* A deep copy is made if "deep" is true or a positive number.
* The number specifies how many levels to copy. Infinity is a valid number.
* This variable is used internally during deep copy.
* @param {function} filter
* A filter function(object) to filter objects that should be copied.
* If it returns true, the copy is performed.
* @returns {object}
*
*/
function objectKeysToLowerCase( input, deep, filter ) {
var idx, key, keys, last, output, self, type, value;
self = objectKeysToLowerCase;
type = typeof deep;
// Convert "deep" to a number between 0 to Infinity or keep special object.
if ( type === 'undefined' || deep === null || deep === 0 || deep === false ) {
deep = 0; // Shallow copy
}
else if ( type === 'object' ) {
if ( !( deep instanceof self ) ) {
throw new TypeError( 'Expected "deep" to be a special object' );
}
}
else if ( deep === true ) {
deep = Infinity; // Deep copy
}
else if ( type === 'number' ) {
if ( isNaN(deep) || deep < 0 ) {
throw new RangeError(
'Expected "deep" to be a positive number, got ' + deep
);
}
}
else throw new TypeError(
'Expected "deep" to be a boolean, number or object, got "' + type + '"'
);
// Check type of input, and throw if null or not an object.
if ( input === null || typeof input !== 'object' ) {
throw new TypeError( 'Expected "input" to be an object' );
}
// Check type of filter
type = typeof filter;
if ( filter === null || type === 'undefined' || type === 'function' ) {
filter = filter || null;
} else {
throw new TypeError( 'Expected "filter" to be a function' );
}
keys = Object.keys(input); // Get own keys from object
last = keys.length - 1;
output = {}; // new object
if (deep) { // only run the deep copy if needed.
if (typeof deep === 'number') {
// Create special object to be used during deep copy
deep =
Object.seal(
Object.create(
self.prototype,
{
input : { value : [] },
output : { value : [] },
level : { value : -1, writable:true },
max : { value : deep, writable:false }
}
)
);
} else {
// Circle detection
idx = deep.input.indexOf( input );
if ( ~idx ) {
return deep.output[ idx ];
}
}
deep.level += 1;
deep.input.push( input );
deep.output.push( output );
idx = last + 1;
while ( idx-- ) {
key = keys[ last - idx ]; // Using [last - idx] to preserve order.
value = input[ key ];
if ( typeof value === 'object' && value && deep.level < deep.max ) {
if ( filter ? filter(value) : value.constructor === Object ) {
value = self( value, deep, filter );
}
}
output[ key.toLowerCase() ] = value;
}
deep.level -= 1;
} else {
// Simple shallow copy
idx = last + 1;
while ( idx-- ) {
key = keys[ last - idx ]; // Using [last - idx] to preserve order.
output[ key.toLowerCase() ] = input[ key ];
}
}
return output;
}
回答2:
I'd use Lo-Dash.transform like this:
var lowerObj = _.transform(obj, function (result, val, key) {
result[key.toLowerCase()] = val;
});
回答3:
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;
}, {});
};
回答4:
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)
}
回答5:
Using forEach seems to be a bit quicker in my tests- and the original reference is gone, so deleting the new one will put it in reach of the g.c.
function keysToLowerCase(obj){
Object.keys(obj).forEach(function (key) {
var k = key.toLowerCase();
if (k !== key) {
obj[k] = obj[key];
delete obj[key];
}
});
return (obj);
}
var O={ONE:1,two:2,tHree:3,FOUR:4,Five:5,SIX:{a:1,b:2,c:3,D:4,E:5}};
keysToLowerCase(O);
/* returned value: (Object) */
{
five:5,
four:4,
one:1,
six:{
a:1,
b:2,
c:3,
D:4,
E:5
},
three:3,
two:2
}
回答6:
Consider lowering case just once, storing it in a lowKey
var:
function keysToLowerCase (obj) {
var keys = Object.keys(obj);
var n = keys.length;
var lowKey;
while (n--) {
var key = keys[n];
if (key === (lowKey = key.toLowerCase()))
continue
obj[lowKey] = obj[key]
delete obj[key]
}
return (obj);
}
回答7:
Here's my recursive version based on one of the above examples.
//updated function
var lowerObjKeys = function(obj) {
Object.keys(obj).forEach(function(key) {
var k = key.toLowerCase();
if (k != key) {
var v = obj[key]
obj[k] = v;
delete obj[key];
if (typeof v == 'object') {
lowerObjKeys(v);
}
}
});
return obj;
}
//plumbing
console = {
_createConsole: function() {
var pre = document.createElement('pre');
pre.setAttribute('id', 'console');
document.body.insertBefore(pre, document.body.firstChild);
return pre;
},
info: function(message) {
var pre = document.getElementById("console") || console._createConsole();
pre.textContent += ['>', message, '\n'].join(' ');
}
};
//test case
console.info(JSON.stringify(lowerObjKeys({
"StackOverflow": "blah",
"Test": {
"LULZ": "MEH"
}
}), true));
Beware, it doesn't track circular references, so you can end up with an infinite loop resulting in stack overflow.
回答8:
ES6 version:
Object.keys(source)
.reduce((destination, key) => {
destination[key.toLowerCase()] = source[key];
return destination;
}, {});
回答9:
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() + '":'
}))
}
回答10:
For all values:
to_lower_case = function(obj) {
for (var k in obj){
if (typeof obj[k] == "object" && obj[k] !== null)
to_lower_case(obj[k]);
else if(typeof obj[k] == "string") {
obj[k] = obj[k].toLowerCase();
}
}
return obj;
}
Same can be used for keys with minor tweaks.
回答11:
I try to do something similar and it might help (Typescript):
function toLowerCaseObject(items: any[]) {
return (items || []).map(x => {
let lowerCased = {}
for (let i in x) {
if (x.hasOwnProperty(i)) {
lowerCased[i.toLowerCase()] = x[i] instanceof Array ? toLowerCaseObject(x[i]) : x[i];
}
}
return lowerCased;
});
}
回答12:
Simplified Answer
For simple situations, you can use the following example to convert all keys to lower case:
Object.keys(example).forEach(key => {
const value = example[key];
delete example[key];
example[key.toLowerCase()] = value;
});
You can convert all of the keys to upper case using toUpperCase()
instead of toLowerCase()
:
Object.keys(example).forEach(key => {
const value = example[key];
delete example[key];
example[key.toUpperCase()] = value;
});