3 回答
TA贡献1798条经验 获得超3个赞
您需要在回调方法中使用return关键字{}:
let allRules = [{ruleName: "a"}, {ruleName: "b"}, {ruleName: "c"}]
let name = "b"
let index = allRules.findIndex(x => {
console.log(x.ruleName)
return x.ruleName == name
})
console.log(index)
一个没有return声明的例子:
let allRules = [{ruleName: "a"}, {ruleName: "b"}, {ruleName: "c"}]
let name = "b"
let index = allRules.findIndex(x => x.ruleName == name)
console.log(index)
TA贡献1830条经验 获得超9个赞
您需要添加退货。
let allRules = [{ruleName: "a"}, {ruleName: "b"}, {ruleName: "c"}]
let name = "b"
let index = allRules.findIndex(x => {
console.log(x.ruleName)
return x.ruleName === name
})
console.log(index)
TA贡献1877条经验 获得超6个赞
正如@Nick Parsons 在评论中所说,你需要return一些东西。
let allRules = [{ruleName: "a"}, {ruleName: "b"}, {ruleName: "c"}]
let name = "b"
let index = allRules.findIndex(x => {
console.log(x.ruleName);
return x.ruleName === name;
})
console.log(index)
添加回答
举报