javascript search array of arrays

2020-01-24 05:43发布

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);
}

10条回答
在下西门庆
2楼-- · 2020-01-24 05:58
function indexOfArray(val, array) {
  var
    hash = {},
    indexes = {},
    i, j;
  for(i = 0; i < array.length; i++) {
    hash[array[i]] = i;
  }
  return (hash.hasOwnProperty(val)) ? hash[val] : -1;
};

I consider this more useful for than containsArray(). It solves the same problem but returns the index rather than just a boolean value of true or false.

查看更多
Rolldiameter
3楼-- · 2020-01-24 06:01

The problem with this is that of object/array equality in Javascript. Essentially, the problem is that two arrays are not equal, even if they have the same values. You need to loop through the array and compare the members to your search key (val), but you'll need a way of accurately comparing arrays.

The easiest way round this is to use a library that allows array/object comparison. underscore.js has a very attractive method to do this:

for (var i = 0; i < ar.length; i++) {
    if (_.isEqual(ar[i], val)) {
        // value is present
    }
}

If you don't want to use another library (though I would urge you to -- or at least borrow the message from the Underscore source), you could do this with JSON.stringify...

var valJSON = JSON.stringify(val);
for (var i = 0; i < ar.length; i++) {
    if (valJSON === JSON.stringify(ar[i]) {
        // value is present
    }
}

This will almost certainly be significantly slower, however.

查看更多
唯我独甜
4楼-- · 2020-01-24 06:03

Use this instead

if (ar.join(".").indexOf(val) > -1) {
 return true;
} else {
 return false;
}
查看更多
看我几分像从前
5楼-- · 2020-01-24 06:04

You can use Array.prototype.some(), Array.prototype.every() to check each element of each array.

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

var val = [3, 566, 23, 79];

var bool = ar.some(function(arr) {
  return arr.every(function(prop, index) {
    return val[index] === prop
  })
});

console.log(bool);

查看更多
家丑人穷心不美
6楼-- · 2020-01-24 06:06

Why don't you use javascript array functions?

function filterArrayByValues(array, values) {
            return array.filter(function (arrayItem) {
                return values.some(function (value) {
                    return value === arrayItem;
                });
            });
        }

Or if your array is more complicated, and you want compare only one property but as result return whole object:

  function filterArrayByValues(array, values, propertyName) {
            return array.filter(function (arrayItem) {
                return values.some(function (value) {
                    return value === arrayItem[propertyName];
                });
            });
        }

More about used functions: filter() and some()

查看更多
时光不老,我们不散
7楼-- · 2020-01-24 06:08

Can you try this?

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

var val = [3,566,23,79];


var sval = val.join("");
for(var i in ar)
{
    var sar = ar[i].join("");
    if (sar==sval) 
    {
        alert("found!");
        break;
    }
}
查看更多
登录 后发表回答