findIndex() 方法返回传入一个测试条件(函数)符合条件的数组第一个元素位置。
findIndex() 方法为数组中的每个元素都调用一次函数执行:
1、当数组中的元素在测试条件时返回 true 时, findIndex() 返回符合条件的元素的索引位置,之后的值不会再调用执行函数。
2、如果没有符合条件的元素返回 -1
原代码:
const dicList = [1, 3]
const itemValue = [1, 2, 3, 4, 5, 6, 7]
var findItemIndex = dicList.findIndex(itemD => { itemD === itemValue })
其中有返回true的, 但是findItemIndex一直返回-1,看了很久才发现满足条件的数据没返回,只要在条件前加return就行了
改为:
const dicList = [1, 3]
const itemValue = [1, 2, 3, 4, 5, 6, 7]
var findItemIndex = dicList.findIndex(itemD => { return itemD === itemValue })
就OK了