Change to show the correct code for future internet traders: visit http://jsperf.com/stackoverflow-for-vs-hasownproperty/5 for comparison
var testVal = 'BigBrownFox', arr = [1,4,'asd','BigBrownFox',9]; if( arr.indexOf('testVal') > -1 ){
To test an array of values ββto exist in another array:
var testVal = ['BigBrownFox'], arr = [1,4,'asd','BigBrownFox',9]; for(var i=0, len=testVal.length; i<len; i++){ if( arr.indexOf(testVal[i]) > -1 ){
Actually, your approach is slightly disabled in both cases.
If you are using an array, use the indexOf function. If a test value exists, it will return its index. Otherwise, it is -1 if not found. No loop is needed at all.
In the case of an object, you are not using .hasOwnProperty. Yes, it does what you need, but its harder and slower because you are making a function call.
Just use
var eObject = {"123456":"0","234567":"0","345678":"0","456789":"0","012345":"0"}; if( '234567' in eObject ){
Hope this helps.
source share