Is there an efficient way to look up case-sensitive JavaScript object name?

There is a specific object in which the exact time of the properties is not known in advance. For example, the property name may be β€œAbC” or β€œAbc” or β€œabc,” etc.

However, I know that there is only one. That is, I know that there can be no property "AbC", as well as the property "abc".

The property name itself is case sensitive. Therefore, if it is stored as an Object.Abc object, and I am looking for aObject.abc, I will not find it.

My object can have 1000 of these properties.

It would be possible, but inefficient, if every time I wanted to perform a search, I compared the value of the lower case of the property I want to find with the smallest value of the property names, for example:

propertyName = inputValue.toLowerCase(); for (var i in theObject) { if (propertyName == i.toLowerCase()); // found a matching property name } 

Does anyone know a smarter way to do this?

For reasons that would take too long to explain, I can't just recreate the object and make all the properties more lowercase. I really understand that this is possible. I could just find

 theObject['inputValue'.toLowerCase()] 

directly. But, as I said, I can’t. Property names in an object are what they are, and they cannot be changed. Asking me why this would be a huge departure from the problem. Take my word for it, that the object is stuck with the names of the properties that it has.

Does anyone know an efficient way to search for a case-insensitive property name in such a situation?

+5
source share
3 answers

As you say, I do not think you want in the Object way loop. And for you, I thought that it was more efficient and simple, and it does not go in cycles.

let's see the fiddle:

https://fiddle.jshell.net/skgkLnx9/

here is an example:

 var theObject = {aBc: 1, BBA: 2, CCCa: 4, Dba: 3}; // if your env is < ES5, you can use a polyfill( like underscore or lodash provided `_.keys` method ) var theKeys = Object.getOwnPropertyNames(theObject).toString(); // or var theKeys = Object.keys(theObject); var getPropValue = function(prop){ var match = new RegExp(prop, 'i').exec(theKeys); return match && match.length > 0 ? theObject[match[0]] : ''; } console.log(getPropValue('abc')) console.log(getPropValue('Dba')) 

I will also review your big data. I also use my code to test an object that has a 500 property that it can directly return. Although when it is very large, there may be a memory problem, I think it can give you an idea about it.

A wish

could help you :)

+3
source

Creating a Jelly example, but perhaps more efficient or understandable:

  var theObject = {aBc: 1, BBA: 2, CCCa: 4, Dba: 3}; var theKeys = Object.getOwnPropertyNames(theObject); var lookup = {}; theKeys.forEach(function(key) { lookup[key.toLowerCase()] = key; }); var getPropValue = function(prop){ return lookup[prop.toLowerCase()]; } console.log(getPropValue('abc')) console.log(getPropValue('Dba')) 
+3
source

And even further than Sigfried:

 var theObject = {aBc: 1, BBA: 2, CCCa: 4, Dba: 3}; var lcKeys = Object.keys (theObject).reduce ( function (keys, k) { keys[k.toLowerCase()] = k; return keys }, {}); function getValue (key) { return theObject[lcKeys[key.toLowerCase ()]] } console.log (getValue ('abc')); console.log (getValue ('Dba')); 
+3
source

Source: https://habr.com/ru/post/1242343/


All Articles