查找数组是否在2D数组中

时间:2017-01-09 01:39:16

标签: javascript

我想知道数组是否在2D数组中。

这就是我的尝试:

var x=[1,2];
var y=[[1,1],[1,2],[2,2],[3,3]];

y.includes(x); //should return true

2 个答案:

答案 0 :(得分:5)

你可以创建一个哈希:

$num_one = 5;
$num_two = 10;
$calc_choice = "add";

// our function would be doing this behind the scene
customEval(5, 10, "add"){
    // Because our add function returns 15 in this case
    echo '15'
}  

答案 1 :(得分:4)

您可以使用链式数组方法执行此操作!

var ar = [
    [1,1],[1,2],[2,2],[3,3]
];

hasDuplicates(ar, [1,"1"]); //false
hasDuplicates(ar, [1,1]); //true

//Use some to determine at least 1 inner array matches
function hasDuplicates(array, valueToCheck) {
    return array.some(function(a, i) {
        //Check each inner arrays index, and verify that it equals on the same index of the array we want to check
        return a.every(function(ax, ix) {
            return valueToCheck[ix] === ax; //triple equals for equality!
        })
    });
}

演示:https://jsfiddle.net/a3rq70hL/1/