简体   繁体   English

是否有一个Javascript函数用于检查数组数组是否包含特定数组?

[英]Is there a Javascript function for checking if an array of arrays contains a specific array?

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. 我正在制作一个Javascript应用程序,我需要为每个包含两个对象的特定数组检查一个数组数组(一个嵌套数组)。 The nested array looks somewhat like this: [[obj1, obj2], [obj2, obj3], [obj3, obj1]] 嵌套数组看起来有点像这样: [[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] . 我尝试这样做: array.includes([obj1, obj2]) ,但这会返回false,即使数组确实包含[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? 我怎样才能做到这一点?

includes compares with === , apparently your search array is only equivalent to one of the entries, not the same actual array. includes===比较,显然你的搜索数组只相当于其中一个条目,而不是相同的实际数组。

You can use some with the callback checking whether every entry in the array being visited matches your search array. 您可以使用some回调检查被访问的数组中的every条目是否与您的搜索数组匹配。

const flag = arrayOfArrays.some(
    array => array.length === search.length && array.every(
        (entry, index) => entry === search[index]
   )
);

Note that this assumes the inner entries are comparable with === . 请注意,这假设内部条目与===相当。 If not, substitute the appropriate way of comparing them. 如果没有,用适当的方式替换它们。

Live Example: 实例:

 function check(arrayOfArrays, search) { return arrayOfArrays.some( array => array.length === search.length && array.every( (entry, index) => entry === search[index] ) ); } console.log( "yes:", check( [[1, 2], [3, 4], [5, 6], [7, 8]], [5, 6] ) ); console.log( "no:", check( [[1, 2], [3, 4], [5, 6], [7, 8]], [6, 5] // Note the different order ) ); 

If you want to find the array in the array of arrays, you can use find instead of some . 如果要在数组数组中找到数组,可以使用find而不是some数组。 If you want to find its index in the array of arrays, you can use findIndex . 如果要在数组数组中找到其索引 ,可以使用findIndex

In JS each array(object) is unique, even if it has the same content as another one: 在JS中,每个数组(对象)都是唯一的,即使它具有与另一个相同的内容:

 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; } 

If you use a package like lodash, it has a method for comparing if two objects are equal by content rather than by the much stricter === comparison: https://lodash.com/docs/4.17.15#isEqual 如果你使用一个包状lodash,它有比较的方法,如果两个对象是通过内容而不是通过更严格相等===比较: https://lodash.com/docs/4.17.15#isEqual

Using _.isEqual , you could then do something like this: 使用_.isEqual ,您可以执行以下操作:

array.findIndex(item => _.isEqual(item, testObject)) >= 0

Lodash might even have a method to directly search an array with a deep-equals comparison too, but the above would get the job done. Lodash甚至可能有一种方法来直接搜索具有深度等于比较的数组,但上面的内容将完成工作。

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

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