Search a value in a multi array JavaScript.


var found = false;

var in_array = function(needle, haystack) {
if(Array.isArray(haystack)) {
for(var x = 0; x < haystack.length; x++) {
if(found) { break; }
if(Array.isArray(haystack[x])) {
in_array(needle, haystack[x]);
} else {
if(haystack[x] === needle) {
found = true;
break;
}
}
}
}
return found;
};

var arr = [[1, 23], [2, 5], [1,5], [-1, -1500], 0];

console.log(in_array(-1, arr));

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s