简体   繁体   English

比较二维数组js

[英]comparing two dimensional array js

I am coding something in JS and I have to test code - I have to check if elements in 2 arrays are the same. 我正在用JS编码,我必须测试代码-我必须检查2个数组中的元素是否相同。 So I've got an array: boreholes = [[66000, 457000],[1111,2222]....]; 因此,我得到了一个数组: boreholes = [[66000, 457000],[1111,2222]....]; and I want to check if this array contain element for eg. 并且我想检查该数组是否包含例如元素。 [66000,457000] so I did: boreholes.indexOf([66000,457000]) but it returns -1, so I iterate trough array by: [66000,457000]所以我做了: boreholes.indexOf([66000,457000])但它返回-1,所以我通过以下方式迭代了槽数组:

for (var i = 0; i< boreholes.length; i++){
 if (boreholes[i] == [66000, 457000]){
  console.log('ok');
  break;
 }
};

but still I've got nothing. 但我一无所有。 Can someone explain me what am I doing wrong? 有人可以解释一下我在做什么错吗?

You are comparing distinct objects. 您正在比较不同的对象。 When comparing objects, the comparison only evaluates to true when the 2 objects being compared are the same object. 当比较的目的,比较只计算结果为true时,2个对象被比较是相同对象。 IE IE浏览器

var a = [1,2,3];
var b = a;
a === b //true
b = [1,2,3];
a === b //false, b is not the same object

To compare arrays like this, you need to compare all of their elements separately: 要比较这样的数组,您需要分别比较它们的所有元素:

for (var i = 0; i < boreholes.length; i++) {
    if (boreholes[i][0] == 66000 && boreholes[i][1] == 457000) {
        console.log('ok');
        break;
    }
}

You cannot compare arrays like array1 == array2 in javascript like you're trying to do here. 您无法像在此处尝试那样在javascript中比较数组array1 == array2类的array1 == array2

Here is a kludge method to compare two arrays: 这是比较两个数组的kludge方法:

function isEqual(array1, array2){
  return (array1.join('-') == array2.join('-'));
}

You can now use this method in your code like: 您现在可以在代码中使用此方法,例如:

for (var i = 0; i< boreholes.length; i++){
 if (isEqual(boreholes[i], [66000, 457000]){
  console.log('ok');
  break;
 }
};

Currently I had the same problem, did it with the toString() method 目前,我有同样的问题,使用toString()方法完成了

var array1 = [1,2,3,[1,2,3]]
var array2 = [1,2,3,[1,2,3]]

array1 == array2 // false
array1.toString() == array2.toString() // true

var array3 = [1,2,3,[1,3,2]]

// Take attention
array1.toString() == array3.toString() // false

You could also doing it with the Underscore.js -framework for functional programming. 你也可以用做Underscore.js -framework函数式编程。

function containsElements(elements) {
  _.find(boreholes, function(ele){ return _.isEqual(ele, elements); });
}

if(containsElements([66000, 457000])) {
  console.log('ok');
}

The question isn't quite clear if there can be more than 2 elements in an array, so this might work 问题还不清楚,数组中是否可以包含两个以上的元素,所以这可能有效

var boreholes = [[66000, 457000],[1111,2222]]; 
var it = [66000, 457000];

function hasIt(boreholes, check) {
  var len = boreholes.length;
  for (var a = 0; a < len; a++) {
    if (boreholes[a][0] == check[0] && boreholes[a][1] == check[1]) {
       // ok
       return true;
    }
  }
  return false;
}

if (hasIt(boreholes, it)) {
  // ok, it has it
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM