I have the following code and need to check if a value exists as an array key.
I can’t seem to generate the index of the key even though it does exist, any help would be great.
Code:
var run = { // store the actions to trigger
block : function() {
console.log('Blocking…');
},
warning : function() {
console.log('Warning…');
}
};
console.log( $.inArray( 'warning' , run ) );
As far as I can see, warning exists inside run{} and should return an index of 1.
Why isn’t the above doesn’t get found (index is returned as -1).
Thanks for reading.
runisn’t an array (it’s just a plain object) so it doesn’t have an index. Even thoughblockcomes beforewarning, objects are order-less, so you can’t say thatrunhas an index of 1.To check if an object has a particular key, simply check:
Or: