3 回答

TA貢獻(xiàn)1798條經(jīng)驗 獲得超3個贊
您需要在回調(diào)方法中使用return關(guān)鍵字{}:
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貢獻(xiàn)1830條經(jīng)驗 獲得超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貢獻(xiàn)1877條經(jīng)驗 獲得超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)
添加回答
舉報