在我的find函数中使用.includes作为条件时遇到了问题。
var a = ["a.example", "b.example1", "c.example2"];
var b = "example1";
a[1].includes(b)
>> true
a[1].indexOf(b)>-1
>>true
a.find( c => { c.includes(b) });
>>undefined
a.find( c => { c.indexOf(b) > -1 });
>>undefined 我的理解是,如果所有数组元素都不符合条件,那么find将返回undefined,或者返回第一个符合条件的元素。但是我不能让这个条件起作用。我做错了什么吗?我希望find返回一个真实的"b.example1"
发布于 2020-02-21 00:03:49
由于.find()回调中充满了使用大括号的箭头函数,因此将return语句放入回调中。
a.find(c => { return c.includes(b) });
a.find(c => { return c.indexOf(b) > -1 });或者完全删除它。
a.find(c => c.includes(b));
a.find(c => c.indexOf(b) > -1);有关进一步的解释,请查看MDN:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions上的箭头函数引用
https://stackoverflow.com/questions/60324013
复制相似问题