Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript search array of arrays

Tags:

Let's say we have the following js array

var ar = [    [2,6,89,45],    [3,566,23,79],    [434,677,9,23] ];  var val = [3,566,23,79]; 

Is there a js builtin function or jQuery one with which you can search the array ar for val?

Thanks

***UPDATE*************

Taking fusion's response I created this prototype

Array.prototype.containsArray = function(val) {     var hash = {};     for(var i=0; i<this.length; i++) {         hash[this[i]] = i;     }     return hash.hasOwnProperty(val); } 
like image 925
Thomas Avatar asked Jun 11 '11 09:06

Thomas


People also ask

How do you find an array of arrays?

Use forEach() to find an element in an array The Array. prototype. forEach() method executes the same code for each element of an array. The code is simply a search of the index Rudolf (🦌) is in using indexOf.

How do you access elements in an array of arrays?

Access Array Elements You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.


1 Answers

you could create a hash.

var ar = [     [2,6,89,45],     [3,566,23,79],     [434,677,9,23] ];  var hash = {}; for(var i = 0 ; i < ar.length; i += 1) {     hash[ar[i]] = i; }  var val = [434,677,9,23];  if(hash.hasOwnProperty(val)) {     document.write(hash[val]); } 
like image 96
fusion Avatar answered Sep 19 '22 15:09

fusion