是否存在Javascript函数,用于检查数组数组是否包含特定数组? [英] Is there a Javascript function for checking if an array of arrays contains a specific array?

查看:63
本文介绍了是否存在Javascript函数,用于检查数组数组是否包含特定数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个Javascript应用程序,需要检查一个数组数组(一个嵌套数组)中每个包含两个对象的特定数组。嵌套数组看起来像这样: [[obj1,obj2],[obj2,obj3],[obj3,obj1]]



我的问题是,即使应用程序的这一部分没有引发任何错误,也无法按预期工作。我试图这样处理: array.includes([obj1,obj2]),但这返回false,即使数组确实包含 [ obj1,obj2]



那为什么不按预期方式工作?在这种情况下有什么更好的方法? p>

编辑:
我想严格比较包含在数组中的对象,无论包含它们的数组的引用如何。我该怎么做?

解决方案

在JS中,每个数组(对象)都是唯一的,即使它与另一个数组(对象)具有相同的内容一个:



  console.log({} == {}); // falseconsole.log([] == []); // falseconsole.log([1,2] == [1,2]); // false  



因此,您必须循环并比较每个元素一对一:



  let arr = [[0,0,0] ,[1,1,1],[2,2,2],] console.log(arrInArr(arr,[3,3,3])); // falsearr [3] = [3,3,3]; //添加了新元素econsole.log(arrInArr(arr,[3,3,3])); // truefunction arrInArr(mama,child){for(让i = 0; i< mama.length; i ++){//甚至不开始比较长度是否不相等if(mama [i] .length! = child.length){继续} let match = 0; //要计算()的每个精确匹配,(j = 0; j< child.length; j ++){if(mama [i] [j] === child [j]){//具有相等的长度,可以使用索引'j'//比较数组的每个元素。匹配++; } else {break; }} if(match === child.length){//如果每个元素都完全匹配,则返回true;否则,返回true。 }}返回false;}  


I'm making a Javascript application where I need to check an array of arrays (a nested array) for specific arrays containing two objects each. The nested array looks somewhat like this: [[obj1, obj2], [obj2, obj3], [obj3, obj1]]

The problem I have is that even though this part of the application doesn't raise any errors, it does not work as intended. I tried to approach it like this: array.includes([obj1, obj2]), but this returns false, even though the array does contain [obj1, obj2].

So why isn't this working the way it's intended, and what's a better approach for this situation?

Edit: I want to check with strict comparison of the objects included in the array, no matter the reference of the array containing them. How can I do that?

解决方案

In JS each array(object) is unique, even if it has the same content as another one:

console.log( {} == {} ); // false
console.log( [] == [] ); // false
console.log( [1, 2] == [1, 2] ); // false

So, you have to loop and compare each element one by one:

let arr = [
  [0, 0, 0], 
  [1, 1, 1],
  [2, 2, 2], 
]

console.log( arrInArr( arr, [3, 3, 3] ) ); // false
arr[3] = [3, 3, 3]; // new elem added
console.log( arrInArr( arr, [3, 3, 3] ) ); // true

function arrInArr(mama, child){
  for( let i = 0; i < mama.length; i++ ){
    // don't even starting to compare if length are not equal
    if( mama[i].length != child.length ){ continue }
    
    let match = 0; // To count each exact match
    for( let j = 0; j < child.length; j++ ){
      if( mama[i][j] === child[j] ){
        // We made sure, that they have equal length, and can use index 'j'
        // to compare each element of arrays. 
        match++;
      } else {
        break;
      }
    }
    if( match === child.length ){ // If each element exactly matched
      return true; 
    }
  }
  return false;
}

这篇关于是否存在Javascript函数,用于检查数组数组是否包含特定数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆