如何检查一个数组是否包含另一个数组? [英] How to check if an array contains another array?
问题描述
我需要二维数组,所以我做了一个嵌套数组,因为 JavaScript 不允许.
它们看起来像这样:
var myArray = [[1, 0],[1, 1],[1, 3],[2, 4]]
如何检查此数组是否包含 vanilla JS 中的特定元素(即这些 [0,1]
数组之一)?
这是我尝试过的,但没有成功(一切都返回 false
)(我在代码段中包含了答案):
var myArray = [[1, 0],[1, 1],[1, 3],[2, 4]]var itemTrue = [2, 4];var itemFalse = [4, 4];函数包含(a,obj){var i = a.length;当我 - ) {如果(a[i] === obj){返回真;}}返回假;}//第一个答案的解决方案函数 isArrayInArray(x, 检查) {for (var i = 0, len = x.length; i < len; i++) {if (x[i][0] === check[0] && x[i][1] === check[1]) {返回真;}}返回假;}//接受答案的解决方案函数 isArrayInArray2(x, 检查) {var 结果 = x.find(function(ele) {返回 (JSON.stringify(ele) === JSON.stringify(check));})返回结果 !=null}console.log("true :" + myArray.includes(itemTrue));console.log("false :" + myArray.includes(itemFalse));console.log("true :" + (myArray.indexOf(itemTrue) != -1));console.log("false :" + (myArray.indexOf(itemFalse) != -1));console.log("true :" + contains(myArray, itemTrue));console.log("false :" + contains(myArray, itemFalse));//第一个答案的解决方案console.log("true :" + isArrayInArray(myArray, itemTrue));console.log("false :" + isArrayInArray(myArray, itemFalse));//接受答案的解决方案console.log("true :" + isArrayInArray2(myArray, itemTrue));console.log("false :" + isArrayInArray2(myArray, itemFalse));
它可能看起来像重复,但我找不到类似的问题.如果是,请随意标记它.
简短易行,将数组字符串化并作为字符串进行比较
function isArrayInArray(arr, item){var item_as_string = JSON.stringify(item);var contains = arr.some(function(ele){返回 JSON.stringify(ele) === item_as_string;});返回包含;}var myArray = [[1, 0],[1, 1],[1, 3],[2, 4]]var item = [1, 0]console.log(isArrayInArray(myArray, item));//如果找到则打印真
检查一些文档这里>
I needed 2d arrays, so I made a nested array since JavaScript doesn't allow them.
They look like this:
var myArray = [
[1, 0],
[1, 1],
[1, 3],
[2, 4]
]
How can I check if this array includes a specific element (i.e. one of these [0,1]
arrays) in vanilla JS?
Here is what I tried, with no success (everything returns false
) (EDIT: I included the answers in the snippet):
var myArray = [
[1, 0],
[1, 1],
[1, 3],
[2, 4]
]
var itemTrue = [2, 4];
var itemFalse = [4, 4];
function contains(a, obj) {
var i = a.length;
while (i--) {
if (a[i] === obj) {
return true;
}
}
return false;
}
// EDIT: first answer's solution
function isArrayInArray(x, check) {
for (var i = 0, len = x.length; i < len; i++) {
if (x[i][0] === check[0] && x[i][1] === check[1]) {
return true;
}
}
return false;
}
// EDIT: accepted answer's solution
function isArrayInArray2(x, check) {
var result = x.find(function(ele) {
return (JSON.stringify(ele) === JSON.stringify(check));
})
return result !=null
}
console.log("true :" + myArray.includes(itemTrue));
console.log("false :" + myArray.includes(itemFalse));
console.log("true :" + (myArray.indexOf(itemTrue) != -1));
console.log("false :" + (myArray.indexOf(itemFalse) != -1));
console.log("true :" + contains(myArray, itemTrue));
console.log("false :" + contains(myArray, itemFalse));
// EDIT: first answer's solution
console.log("true :" + isArrayInArray(myArray, itemTrue));
console.log("false :" + isArrayInArray(myArray, itemFalse));
// EDIT: accepted answer's solution
console.log("true :" + isArrayInArray2(myArray, itemTrue));
console.log("false :" + isArrayInArray2(myArray, itemFalse));
It could look like duplicate but I couldn't find a similar question. If it is, feel free to tag it as such.
Short and easy, stringify the array and compare as strings
function isArrayInArray(arr, item){
var item_as_string = JSON.stringify(item);
var contains = arr.some(function(ele){
return JSON.stringify(ele) === item_as_string;
});
return contains;
}
var myArray = [
[1, 0],
[1, 1],
[1, 3],
[2, 4]
]
var item = [1, 0]
console.log(isArrayInArray(myArray, item)); // Print true if found
check some documentation here
这篇关于如何检查一个数组是否包含另一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!