Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
115
rated 0 times [  117] [ 2]  / answers: 1 / hits: 85689  / 12 Years ago, tue, september 18, 2012, 12:00:00

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;
}


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.


More From » object

 Answers
16

Compare all the properties of obj with prop.



var objSetter = function(prop,val){
prop = (prop + ).toLowerCase();
for(var p in obj){
if(obj.hasOwnProperty(p) && prop == (p+ ).toLowerCase()){
obj[p] = val;
break;
}
}
}

[#83031] Monday, September 17, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dustin

Total Points: 599
Total Questions: 105
Total Answers: 106

Location: Belarus
Member since Tue, Mar 14, 2023
1 Year ago
dustin questions
;