简体   繁体   中英

binding to Array.includes [javascript]

I'm trouble with using this in find if any element in array2 exists in array1:

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.

[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 . Where the second argument is starting index to search from.

arr.includes(searchElement, fromIndex)

So every next call asks if your first array includes given value starting the given index.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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