Javascript javascript搜索数组数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6315180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript search array of arrays
提问by Thomas
Let's say we have the following js array
假设我们有以下 js 数组
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 arfor val?
是否有一个js内置函数或jQuery的一个,使用它可以搜索阵列AR的VAL?
Thanks
谢谢
***UPDATE*************
** *更新*** *** *** *** *
Taking fusion'sresponse I created this prototype
考虑到fusion的反应,我创造了这个原型
Array.prototype.containsArray = function(val) {
var hash = {};
for(var i=0; i<this.length; i++) {
hash[this[i]] = i;
}
return hash.hasOwnProperty(val);
}
回答by fusion
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]);
}
回答by Yeldar Kurmangaliyev
You can also use a trick with JSON serializing. It is short and simple, but kind of hacky.
It works, because "[0,1]" === "[0,1]"
.
您还可以使用 JSON 序列化技巧。它简短而简单,但有点hacky。
它有效,因为"[0,1]" === "[0,1]"
.
Here is the working demo snippet:
这是工作演示片段:
Array.prototype.indexOfForArrays = function(search)
{
var searchJson = JSON.stringify(search); // "[3,566,23,79]"
var arrJson = this.map(JSON.stringify); // ["[2,6,89,45]", "[3,566,23,79]", "[434,677,9,23]"]
return arrJson.indexOf(searchJson);
};
var arr = [
[2,6,89,45],
[3,566,23,79],
[434,677,9,23]
];
document.body.innerText = arr.indexOfForArrays([3,566,23,79]);
回答by Hans
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
.
我认为这比containsArray()
. 它解决了同样的问题,但返回索引而不仅仅是true
or的布尔值false
。
回答by sudipto
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;
}
}
回答by lonesomeday
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.
问题在于 Javascript 中的对象/数组相等。本质上,问题在于两个数组不相等,即使它们具有相同的值。您需要遍历数组并将成员与您的搜索键 ( val
)进行比较,但您需要一种准确比较数组的方法。
The easiest way round this is to use a library that allows array/object comparison. underscore.jshas a very attractive method to do this:
解决此问题的最简单方法是使用允许数组/对象比较的库。underscore.js有一个非常有吸引力的方法来做到这一点:
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
...
如果您不想使用另一个库(尽管我会敦促您 - 或者至少从 Underscore 源借用该消息),您可以使用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.
然而,这几乎肯定会慢得多。
回答by Talha Ahmed Khan
I guess there is no such JS functionality available. but you can create one
我想没有这样的 JS 功能可用。但你可以创建一个
function arrEquals( one, two )
{
if( one.length != two.length )
{
return false;
}
for( i = 0; i < one.length; i++ )
{
if( one[i] != two[i] )
{
return false;
}
}
return true;
}
回答by guest271314
You can use Array.prototype.some()
, Array.prototype.every()
to check each element of each array.
您可以使用Array.prototype.some()
,Array.prototype.every()
检查每个数组的每个元素。
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);
回答by farynaa
You can use toString convertion to compare elements
您可以使用 toString 转换来比较元素
var ar = [
[2,6,89,45],
[3,566,23,79],
[434,677,9,23]
];
var val = [3,566,23,79];
s = !ar.every(a => (a.toString() != val.toString()));
console.log(s) // true
回答by GokulRaj K.N.
Use this instead
改用这个
if (ar.join(".").indexOf(val) > -1) {
return true;
} else {
return false;
}
回答by Micha? Jarzyna
Why don't you use javascript array functions?
为什么不使用 javascript 数组函数?
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];
});
});
}