Most efficient javascript function to find an element in an array? and explain this tricky code

What could be the most efficient way to find an element in an array that is logical and understandable to a web developer?

I came across this piece of code:

var inArray = function(a, b, c, d) { for (c in b) d |= b[c] === a; return !!d }; 

It works great. Can someone please explain the code to me?
I also met the very same question that my duplicate could make. But my real question is explaining the code above and why the bitwise operator was used in it.
Also, could there be a way without any loops or iteration to get the index of an element in an array?

+4
source share
4 answers
 var inArray = function(a, b, c, d) { for (c in b) d |= b[c] === a; return !!d }; 

This is terrible code, and you should run away from it. Bitwise operators are completely unnecessary here, and the parameters c and d do not make any sense (as Raymond Chen noted, the author of the code most likely did this for the safe space for declaring local variables - a problem, although it is that if true is passed for d , the code abruptly interrupted, and additional parameters destroy any understanding that would provide an idea of ​​the declaration).

I will explain the code, but first here is the best option:

 function inArray(arr, obj) { for (var i = 0; i < arr.length; ++i) { if (arr[i] === obj) { return true; } } return false; } 

Note that this depends on whether the array is the actual array. You can use a loop like for (k in arr) to generalize it to all objects.


In any case, explain:

 for (c in b) d |= b[c] === a; 

This means that for each key in b (stored in c ) we will check if b[c] === a . In other words, we do a linear scan over the array and check each element on a .

d |= val is bitwise or. Bits with a high val value will be set to d . This is easier to illustrate in languages ​​where bits are more affected than in JS, but their simple illustration is:

 10011011 01000001 -------- 11011011 

This is simply OR'ing each individual bit with the same location bit in a different value.

The reason this abuse is because it overthrows the code and depends on strange implicit garbage.

x === y returns a boolean value. The logical value used in bitwise terms makes little sense. However, what happens is that the logical value is converted to a non-zero value (possibly 1 ).

Similarly, undefined will be d . This means that d will be passed to 0 for bitwise material.

0 | 0 = 0 0 | 0 = 0 , but 0 | 1 = 1 0 | 1 = 1 . So basically it is glorified:

 for (c in b) d = (d || (b[c] === a)); 

As for !!x , which is just used to put something into a bool. !x will take x, implicitly drop it into bool and then cancel. Optional ! then denies it again. So it most likely implicitly results in bool ( !!x being true means that x is at least weakly true ( 1 , "string" , etc.), A !!x implies that x at least weakly false ( 0 , "" , etc.).


This answer offers some more options. Please note that they are all designed to indexOf native indexOf , which will almost certainly be faster than anything we can encode in script -land.

+5
source

This code is pretty poorly written and barely readable. I would not qualify him as "amazing" ...

Here, the rewritten version will hopefully be more readable:

 function inArray (aElt, aArray) { var key; var ret = false; for (key in aArray) ret = ret || (aArray[key] === aElt); return ret; } 

The for loop looks like the most natural way to do this. You can do this recursively, but it doesn't seem like the easiest approach.

+2
source

What could be the most efficient way to find an element in an array that is logical and understandable to a web developer?

Either the native method, haystack.indexOf(needle) !== -1 , or a cyclic method that returns as soon as it can

 function inArray(needle, haystack) { var i = haystack.length; while (i) if (haystack[--i] === needle) return true; return false; }; 
+2
source

You asked for a recursive version

 var inArray = function(arr, needle, index) { index = index || 0; if (index >= arr.length) return false; return arr[index] === needle ? true : inArray(arr, needle, index + 1); } console.log(inArray([1,5,9], 9)); 
+2
source

All Articles