[英]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
,也适用于null
和undefined
)。
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但是对于不可变对象,比如string和number ,可以直接检查,比如
["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.