Access JavaScript property case-insensitively?

Asked
Active3 hr before
Viewed126 times

8 Answers

accesspropertyjavascript
90%

Here is a nice recursive function that allows you to traverse a javascript object in a case-insensitive way:,Case-insensitive setting:,Case-insensitive getting:, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

Try this:

var myObject = {
   "mIxeDCaSEKeY": "value"
};

var searchKey = 'mixedCaseKey';
myObject[Object.keys(myObject).find(key => key.toLowerCase() === searchKey.toLowerCase())];

If you want it as a function:

/**
 * @param {Object} object
 * @param {string} key
 * @return {any} value
 */
function getParameterCaseInsensitive(object, key) {
   return object[Object.keys(object)
      .find(k => k.toLowerCase() === key.toLowerCase())
   ];
}

If you need to support older browsers, then you can use filter instead:

function getParameterCaseInsensitive(object, key) {
   return object[Object.keys(object).filter(function(k) {
      return k.toLowerCase() === key.toLowerCase();
   })[0]];
}
load more v
88%

Approach 1: In this approach, the key is passed to the function and while comparing, Use toLowerCase() method to transform the passed key as well as object’s key to lowercase. Then compare the keys and if they are same then return its value.,Approach 2: The approach here is same as in previous example, The Object.keys() method is used and the operation is performed in a compact manner.,How to access object’s properties without case-sensitivity, In this articles we are discussing how to access object property case-insensitively in JavaScript.,Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

72%

Here is a nice recursive function that allows you to traverse a javascript object in a case-insensitive way:,Assume I have an object:,No problems there, except for that prop needs to be case insensitive in case the property name is passed into the function as, say, Foo instead of foo.,I need to access a property of that object dynamically like so:

Assume I have an object:

var obj = {
   foo: "bar",
   fizz: "buzz"
};

I need to access a property of that object dynamically like so:

var objSetter = function(prop, val) {
   obj[prop] = val;
}
load more v
65%

JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters. The while keyword, for example, must be typed “while”, not “While” or “WHILE”. Similarly, online, Online, OnLine, and ONLINE are four distinct variable names. ,View all O’Reilly videos, Superstream events, and Meet the Expert sessions on your home TV.,Take O’Reilly with you and learn anywhere, anytime on your phone and tablet.,O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.

JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters. The while keyword, for example, must be typed “while”, not “While” or “WHILE”. Similarly, online, Online, OnLine, and ONLINE are four distinct variable names.

while

JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters. The while keyword, for example, must be typed “while”, not “While” or “WHILE”. Similarly, online, Online, OnLine, and ONLINE are four distinct variable names.

online

JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters. The while keyword, for example, must be typed “while”, not “While” or “WHILE”. Similarly, online, Online, OnLine, and ONLINE are four distinct variable names.

Online

JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters. The while keyword, for example, must be typed “while”, not “While” or “WHILE”. Similarly, online, Online, OnLine, and ONLINE are four distinct variable names.

OnLine

JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters. The while keyword, for example, must be typed “while”, not “While” or “WHILE”. Similarly, online, Online, OnLine, and ONLINE are four distinct variable names.

ONLINE
load more v
75%

However, if it’s literally case-insensitive keys, JS allows it, this is how you do that:,And use myObj instead of obj. Note this is normally a super bad idea: it will work perfectly, and you will get a object that accepts case insensitive keys, but it is reflection, and you make things incredibly non-obvious.,However, as @DanCouper (who beat me to the punch AGAIN) has said, it’s a terrible idea to do this. It may make it easier to avoid having to remember whether a property name is upper or lower or mix case, but it also makes it very easy to accidentally overwrite properties, and it’s encouraging lazy coding.,I really have to thank you for this question. It was a fun challenge to learn about, but the answer is, oddly, YEP! Javascript has a fun thing called a Proxy. You can use it to define handlers for object properties. Here are some reasons you might use it:

For example, here would be psuedo code what I am asking:

var object = {
   "A" || "a": 97,
   "B" || "b": 98
}
load more v
40%

As you suspected, running checks to make sure that uploaded files meet your standards is probably necessary. You could either automatically force a particular case, or just return a list of non-compliant fields and the rule the violate to the user to correct. You likely have more requirements beyond just the case sensitivity (like required fields) that you'll want to implement.,Connect and share knowledge within a single location that is structured and easy to search.,Please be sure to answer the question. Provide details and share your research!, You'll likely need a server that receives the shapefile, pre-processes it, and then uploads to GeoServer. – Emily Dec 21 '17 at 17:28

Given that Feature#getProperties returns a plain JavaScript Object you could use a function (similar to those proposed here: https://stackoverflow.com/a/33159592/526860) if you're only interested in reading properties:

function getProperty(obj, prop) {
   var key;
   for (key in obj) {
      if (key.toLowerCase() == prop.toLowerCase()) {
         return obj[key];
      }
   }
}
22%

var array = ['I', 'hAve', 'theSe', 'ITEMs'],
   query = 'these',
   result = array.findIndex(item => query.toLowerCase() === item.toLowerCase());

console.log(result); // 2
load more v
60%

I need to access a property of that object dynamically like so:,No problems there, except for that prop needs to be case insensitive in case the property name is passed into the function as, say, Foo instead of foo.,So how can I point to an object's property by name without regard to case? I would like to avoid iterating the entire object if possible.,For this, I prefer using the prototype over a standalone function just for ease of use and expressiveness. I just don't like funneling objects into functions if I don't have to.

Assume I have an object:

var obj = {
   foo: "bar",
   fizz: "buzz"
};

I need to access a property of that object dynamically like so:

var objSetter = function(prop, val) {
   obj[prop] = val;
}
load more v

Other "access-property" queries related to "Access JavaScript property case-insensitively?"