![](/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.