第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

findIndex() 總是在帶有對象的數(shù)組上返回-1

findIndex() 總是在帶有對象的數(shù)組上返回-1

泛舟湖上清波郎朗 2022-01-07 10:49:39
我沒有找到以下問題的解決方案。在這種情況下,索引一直返回 -1 而不是 1。任何人都可以幫助我嗎?let allRules = [{ruleName: "a"}, {ruleName: "b"}, {ruleName: "c"}]let name = "b"let index = allRules.findIndex(x => {  console.log(x.ruleName)  x.ruleName === name})console.log(index)
查看完整描述

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)


查看完整回答
反對 回復(fù) 2022-01-07
?
慕標(biāo)琳琳

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)


查看完整回答
反對 回復(fù) 2022-01-07
?
慕哥9229398

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)


查看完整回答
反對 回復(fù) 2022-01-07
  • 3 回答
  • 0 關(guān)注
  • 729 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號