简体   繁体   English

绑定到Array.includes [javascript]

[英]binding to Array.includes [javascript]

I'm trouble with using this in find if any element in array2 exists in array1: 我很难在array1中是否存在array2中的任何元素的查找中使用它:

array2 = [].includes.bind([1,2,3]);
array2(1);   // returns true
array2(9);   // returns false

[5,6,7,8,9].some(array2);        // returns false  GOOD
[3,6,7,8,9].some(array2);        // returns true   GOOD
[6,7,8,9,3].some(array2);        // returns false  OOPS!

[6,7,8,9,3].some(function(i){return array2(i)});  //returns true

Obviously I know one solution to the problem, but I want to know why the [...].some(array2) doesn't work completely. 显然,我知道该问题的一种解决方案,但是我想知道为什么[...].some(array2)不能完全正常工作。

[6,7,8,9,3].some(array2) doesn't work because Array.prototype.some calls check function with 3 arguments: value, index, entire array while Array.prototype.includes takes 2 arguments . [6,7,8,9,3].some(array2)不起作用,因为Array.prototype.some调用检查函数具有3个参数:值,索引,整个数组,而Array.prototype.includes需要2个参数 Where the second argument is starting index to search from. 第二个参数是从中开始搜索的索引。

arr.includes(searchElement, fromIndex) arr.includes(searchElement,fromIndex)

So every next call asks if your first array includes given value starting the given index. 因此,每个下一个调用都会询问您的第一个数组是否包括从给定索引开始的给定值。

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

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