[英]Check if an array includes an array in javascript
The javascript includes
function can be used to find if an element is present in an array. javascript
includes
函数可用于查找元素是否存在于数组中。 Take the following example:举个例子:
var arr = ['hello', 2, 4, [1, 2]]; console.log( arr.includes('hello') ); console.log( arr.includes(2) ); console.log( arr.includes(3) ); console.log( arr.includes([1, 2]) );
Passing 'hello'
or 2
to the function returns true
, as both are present in the array arr
.将
'hello'
或2
传递给函数会返回true
,因为两者都存在于数组arr
中。
Passing 3
to the function returns false
because it is not present in the array.将
3
传递给函数会返回false
,因为它不存在于数组中。
However, why does arr.includes([1, 2])
return false
as well, even though this is equal to the last element in the array?但是,为什么
arr.includes([1, 2])
也返回false
,即使这等于数组中的最后一个元素? And if this method does not work, how else can I find whether my array includes the item [1, 2]
?如果这种方法不起作用,我还能如何找到我的数组是否包含项目
[1, 2]
?
.includes()
method uses sameValueZero equality algorithm to determine whether an element is present in an array or not. .includes()
方法使用sameValueZero 相等算法来确定一个元素是否存在于数组中。
When the two values being compared are not numbers, sameValueZero
algorithm uses SameValueNonNumber algorithm.当被比较的两个值不是数字时,
sameValueZero
算法使用SameValueNonNumber算法。 This algorithm consists of 8 steps and the last step is relevant to your code , ie when comparison is made between two objects.该算法由 8 个步骤组成,最后一步与您的代码相关,即在两个对象之间进行比较时。 This step is:
这一步是:
- Return true if x and y are the same Object value.
如果 x 和 y 是相同的 Object 值,则返回 true。 Otherwise, return false.
否则,返回假。
So in case of objects, SameValueZero
algorithm returns true only if the two objects are same .因此,对于对象,
SameValueZero
算法仅在两个对象相同时才返回 true 。
In your code, since the array [1, 2]
inside arr
array is identically different to [1, 2]
that you passed to .includes()
method, .includes()
method can't find the array inside arr
and as a result returns false
.在您的代码中,由于
arr
数组中的数组[1, 2]
[1, 2]
与您传递给.includes()
方法的 [1, 2] 完全相同,因此.includes()
方法无法在arr
中找到该数组,并且作为结果返回false
。
The Array#includes
checks by shallow comparison, so in your case the string and numbers are primitives there is only ever a single instance of them so you get true
from Array#includes
. Array#includes
通过浅比较进行检查,因此在您的情况下,字符串和数字是原语,它们只有一个实例,因此您可以从Array#includes
中得到true
的结果。
But when you check for array, you are passing a new array instance which is not the same instance in the array you are checking so shallow comparison fails.但是当您检查数组时,您传递的是一个新的数组实例,该实例与您检查的数组中的实例不同,因此浅比较失败。
To check for an array is included in another array first check if it is an array then do a deep comparison between the arrays.要检查一个数组是否包含在另一个数组中,首先检查它是否是一个数组,然后在数组之间进行深度比较。
var arr = ['hello', 2, 4, [1, 2]]; const includesArray = (data, arr) => { return data.some(e => Array.isArray(e) && e.every((o, i) => Object.is(arr[i], o))); } console.log(includesArray(arr, [1, 2]));
But if you keep the reference to the array [1, 2]
and search with the reference the Array#includes
works as in this case the shallow comparison works perfectly (obeying same value zero algorithm ):但是,如果您保留对数组
[1, 2]
的引用并使用引用搜索Array#includes
就像在这种情况下一样,浅层比较可以完美地工作(遵循相同的值零算法):
const child = [1, 2]; const arr = ['hello', 2, 4, child]; console.log(arr.includes(child));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.