簡體   English   中英

如何修復警告“預計在箭頭 function 數組回調返回中返回一個值”

[英]How fix warning “Expected to return a value in arrow function array-callback-return”

這是我的代碼:

form.listPrice.map(list => {
  if (list.id === listId) {
    form.active = true
    listPrice = parseInt(list.price)
    if (list.offerPrice) {
      listofferPrice = parseInt(list.offerPrice)
    } else {
      listofferPrice = null
    }
  }
})

和這里:

n.listPrice.map(list => {
  if (list.id === listPrice) {
    valid = true;
    n.active = true;
    n.showPrice.price = list.price;
    n.showPrice.offerPrice = list.offerPrice;
    n.ladder = list.ladder;
  }

和這個 output 一樣的警告:

預計在箭頭 function 中返回一個值 array-callback-return

使用.map不正確 .map僅應在您想從舊數組構造數組時使用,但您的代碼沒有這樣做 - 您只是在執行副作用 - form.activelistofferPrice的設置。

第一步是使用forEachfor..of代替,例如:

for (const list of form.listPrice) {
    if (list.id === listId) {
        form.active = true
        listPrice = parseInt(list.price)
        if (list.offerPrice) {
            listofferPrice = parseInt(list.offerPrice)
        } else {
            listofferPrice = null
        }
    }
}

但由於看起來您正試圖在數組中找到一個可能的單個匹配值,因此.find會更合適:

const found = form.listPrice.find(list => list.id === listId);
if (found) {
    form.active = true
    listPrice = parseInt(found.price)
    if (found.offerPrice) {
        listofferPrice = parseInt(found.offerPrice)
    } else {
        listofferPrice = null
    }
}
const found = n.listPrice.find(list => list.id === listPrice);
if (found) {
    valid = true;
    n.active = true;
    n.showPrice.price = found.price;
    n.showPrice.offerPrice = found.offerPrice;
    n.ladder = found.ladder;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM