【发布时间】:2017-11-03 20:09:28
【问题描述】:
给定以下数组:
const x = [2, 14, 54, 109, 129, 136, 165, 312, 320, 330, 335, 348, 399, 440, 450, 461, 482, 501, 546, 547, 549, 559, 582, 584, 615, 620, 647, 682];
const y = [539, 681, 682, 683];
使用 node v 7.3.0 我观察到以下意外行为:
[> x.find(y.includes, y);
undefined
[> y.find(x.includes, x);
682
示例片段:
const x = [2, 14, 54, 109, 129, 136, 165, 312, 320, 330, 335, 348, 399, 440, 450, 461, 482, 501, 546, 547, 549, 559, 582, 584, 615, 620, 647, 682];
const y = [539, 681, 682, 683];
console.log(x.find(y.includes, y))
console.log(y.find(x.includes, x))
但是像x.find(element => y.includes(element)); 这样的代码总是能按预期找到元素。
我不明白为什么只使用 find 和 includes 的两个调用会产生不同的结果,如果有人知道解释会很高兴。
【问题讨论】:
-
find() 和 includes() 的参数不同。
标签: javascript arrays ecmascript-6