简体   繁体   English

为什么不能将 Array#includes 用于嵌套数组?

[英]Why can't I use Array#includes for a nested array?

Why does the following line return false in Javascript:为什么以下行在 Javascript 中返回false

 [[1,2,3], [1,2,4]].includes([1,2,3]);

What is the underlying logic behind that?这背后的底层逻辑是什么?

includes compares using SameValueZero equality algorithm. includes使用SameValueZero相等算法的比较。 (As mentioned in developer.mozilla.org ). (如developer.mozilla.org 中所述)。 When searching for objects (array is object as well), it will match only references to the same object.在搜索对象时(数组也是对象),它只会匹配对同一对象的引用。

Additionally, Javascript arrays are objects and you can't simply use the equality operator == to understand if the content of those objects is the same.此外,Javascript 数组是对象,您不能简单地使用相等运算符==来了解这些对象的内容是否相同。 The equality operator will only test if two object are actually exactly the same instance (eg myObjVariable==myObjVariable , works for null and undefined too).相等运算符只会测试两个对象是否实际上是完全相同的实例(例如myObjVariable==myObjVariable ,也适用于nullundefined )。

Both [1,2,3] expressions create a new array object. [1,2,3]两个表达式都创建了一个新的数组对象。 Even though the contents are the same, the objects themselves are different.即使内容相同,对象本身也不同。

See for example this:例如,请参阅:

 const array1 = [1, 2, 3]; const array2 = [1, 2, 3]; console.log(array1 == array2); // false, different objects

 const array = [1, 2, 3]; console.log(array == array); // true, same object

.includes check the equity for every value in the array. .includes检查数组中每个值的权益。 In JavaScript, two arrays with the same values are not equivalent.在 JavaScript 中,具有相同值的两个数组是不等价的。 See this thread for more details: How to compare arrays in JavaScript?有关更多详细信息,请参阅此线程: 如何比较 JavaScript 中的数组?

You could do this to check if an array contains an array.您可以这样做来检查数组是否包含数组。 I use Lodash for the equity comparison and the .some property to check if one element in the array returns true.我使用 Lodash 进行股权比较,并使用.some属性检查数组中的一个元素是否返回 true。

 console.log( [[1,2,3], [1,2,4]].some((array) => _.isEqual(array, [1,2,3])) )
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>

Because they are mutable.因为它们是可变的。 If you want to check for array, you need to check by variable.如果要检查数组,则需要按变量检查。

var a = [1,2];
var b = a;
 [a].includes(b);

When you check for [[1,2,]].includes([1,2,3]), it returns false, because they are treated as two different objects;当您检查 [[1,2,]].includes([1,2,3]) 时,它返回 false,因为它们被视为两个不同的对象; ie [1,2] == [1,2] returns false.即 [1,2] == [1,2] 返回假。

However, for immutable objects, such as string and number , you can check directly, such as但是对于不可变对象,比如stringnumber ,可以直接检查,比如

["a", "b"].includes("a")  //true
[1, 2].includes(1) //true
"a" == "a"   // true

You can do it using only Array.some() ( Array.prototype.some() more precisely) method like the followings您可以仅使用Array.some() (更准确地说是Array.prototype.some() )方法,如下所示

 console.log([[1,2,3], [1,2,4]].some(e => e[0] === 1 && e[1] === 2 && e[2] === 3)); // would return 'true' console.log([[1,2,3], [1,2,4]].some(e => e[0] === 1 && e[1] === 2 && e[2] === 4)); // would return 'true' console.log([[1,2,3], [1,2,4]].some(e => e[0] === 1 && e[1] === 2 && e[2] === 5)); // would return 'false'

暂无
暂无

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

相关问题 不能在对象字面量中使用像 .includes 这样的数组函数 - Can't use array functions like .includes in Object literals 我可以使用 .includes() 来确定数组是否包含基于字符串数组而不是单个字符串的某个元素? - Can I use .includes() to determines whether an array includes a certain element based on an array of strings instead of a single string? Arr.includes(item)-我可以与多维数组一起使用吗? - Arr.includes(item) - Can I Use with a Multidimensional Array? 为什么我不能使用array.indexOf(this.innerHTML)? - Why Can't I use array.indexOf(this.innerHTML)? 为什么我不能在 Array.isArray 上成功使用“调用”? - Why can't I successfully use 'call' on Array.isArray? 为什么我不能在 while 循环中使用数组作为“指定条件”? - Why can't I use an array as the 'specified condition' in a while loop? Javascript为什么我不能使用这样的数组,如何使用计算出的数字访问数组 - Javascript why can't i use an array like this and how can i access the array with a calculated number 如何使用 Array includes() 方法检查数组中是否存在值? - How can i use Array includes() Method to check value exist in array? 如何使用map将数组/嵌套数组合并为div? - How can I use map to combine an array/nested array into a div? 为什么我不能使用Array.join.call代替Array.prototype.join.call - Why can't I use Array.join.call in place of Array.prototype.join.call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM