![](/img/trans.png)
[英]How to fix "Expected to return a value in arrow function array-callback-return"?
[英]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.active
和listofferPrice
的設置。
第一步是使用forEach
或for..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.