为什么我不能使用Array#includes作为嵌套数组? [英] Why can't I use Array#includes for a nested array?

查看:106
本文介绍了为什么我不能使用Array#includes作为嵌套数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以下行在Javascript中返回 false

Why does the following line return false in Javascript:

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

背后的基本逻辑是什么?

What is the underlying logic behind that?

推荐答案

包括使用 SameValueZero 等式算法。 (如 developer.mozilla.org 中所述)。搜索对象时(数组也是对象),它只匹配对同一对象的引用。

includes compares using SameValueZero equality algorithm. (As mentioned in developer.mozilla.org). When searching for objects (array is object as well), it will match only references to the same object.

此外,Javascript数组是对象,你不能简单地使用等于运算符 == ,以了解这些对象的内容是否相同。等于运算符只会测试两个对象实际上是否完全相同(例如 myObjVariable == myObjVariable ,适用于 null undefined 。)

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. The equality operator will only test if two object are actually exactly the same instance (e.g. myObjVariable==myObjVariable, works for null and undefined too).

这篇关于为什么我不能使用Array#includes作为嵌套数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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