Access JavaScript property case-insensitively?
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;
}
}
}
How to case-insensitively set value in object in Javascript?
you need to search for the key by comparing it to a lowercase version of it.
If no key was found, set the key to a default lowercase value: perm city
const data = { 'perm city': { value: 'asda' }};
console.log(data);
const defaultKey = 'perm city';
const keys = Object.keys(data);
let foundKey = keys.find((key) => key.toLowerCase() === defaultKey);
foundKey = foundKey || defaultKey;
data[foundKey] = 'PIZZA';
console.log(data);
How to check for case insensitive in JavaScript object.hasOwnProperty(value);
You actually won't use object.hasOwnProperty()
here because that method tests the properties as their names are currently cased. You need the ability to modify the case of the property in question, for the purposes of the test.
For that reason, you can't just check a pre-existing property, you need to loop over the the property names and check them against a supplied value...
Just check a lower-case object property name against a forced lower-case comparison value. Also, you had some syntax errors and unnecessary code. See comments for details.
var obj={ FOO: 10, Special: true};
function dupCheck(o, val){
var duplicateFlag = false; // Identifier names can't contain hyphens (-) for(var prop in o){ // force property name and value to lower case for comparison if(prop.toLowerCase() === val.toLowerCase()){ duplicateFlag = true; break; } // No else branch needed because duplicate starts off false } return duplicateFlag;}
console.log(dupCheck(obj, "foo")); // trueconsole.log(dupCheck(obj, "SPECIAL")); // trueconsole.log(dupCheck(obj, "somethingElse"));// false
JavaScript object check for key -- but ignore case
Long Code But Faster
var key, keys = Object.keys(obj);
var n = keys.length;
var objArr=[]
while (n--) {
objArr.push(keys[n].toLowerCase());
}
if (objArr.indexOf(key)!==-1) {
//other stuff on obj
}
Short Code, Less Efficient
var objTemp = JSON.parse(JSON.stringify(obj).toLowerCase());
Now check in
if (key in objTemp) {
//other stuff on obj
}
javascript includes() case insensitive
You can create a RegExp from filterstrings
first
var filterstrings = ['firststring','secondstring','thridstring'];
var regex = new RegExp( filterstrings.join( "|" ), "i");
then test
if the passedinstring
is there
var isAvailable = regex.test( passedinstring );
Object Prototype: Case Insensitive Getter On All Properties
So, after following @apsillers comment, I did solve my problem (I only needed support for lower-case and camel-case. This is not what I would consider ideal and does not actually answer my question of making a case-insensitive Object property, but I should share:
function makeCaseInsensitiveObject(obj)
{
var keys;
function PropertyScope(iObj, key, val)
{
var value = val;
var _get = function()
{
return value;
};
var _set = function(v)
{
value = v;
};
Object.defineProperty(iObj, key, {
get: _get,
set: _set
});
Object.defineProperty(iObj, key.toLowerCase(), {
get: _get,
set: _set
});
};
if(Object.keys)
keys = Object.keys(obj);
else
keys = getObjectKeys(obj);
for(var i = 0; i < keys.length; i++)
{
if(typeof keys[i] === 'string')
{
PropertyScope(obj, keys[i], obj[keys[i]]);
}
}
return obj;
};
Be aware that the case-insensitivity here will only apply to existing object properties, not any new ones.
Thanks everybody!
Related Topics
In R, How to Display Value on the Links/Paths of Sankey Graph
Rails 4 Client Side Validation
Javascriptcore Call Function with Callback Swift
Performance of Key Lookup in JavaScript Object
Headless Browser for Python (JavaScript Support Required!)
Passing Array of Objects from Js to Rails
Pure JavaScript Send Post Data Without a Form
How to Use Protractor on Non Angularjs Website
Protractor E2E Test Case for Downloading PDF File
What Is the "Best" Way to Get and Set a Single Cookie Value Using JavaScript
How to Render a Partial from JavaScript
Understanding Service Worker Scope
Losing "This" Context in JavaScript When Passing Around Members
What's the Best Way to Calculate Date Difference in JavaScript
Javascript:Send JSON Object with Ajax
Elliptic Curve Cryptography with Sjcl in Js and Openssl in Ruby
Determine Which Element the Mouse Pointer Is on Top of in JavaScript