[英]How fix warning “Expected to return a value in arrow function array-callback-return”
This is my code:这是我的代码:
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
}
}
})
And here:和这里:
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;
}
And this output the same warning:和这个 output 一样的警告:
Expected to return a value in arrow function array-callback-return
预计在箭头 function 中返回一个值 array-callback-return
You are using .map
incorrectly .您使用
.map
不正确。 .map
should be used only when you want to construct a new array from an old array, but your code is not doing that - you're only carrying out side-effects - the setting of form.active
and listofferPrice
. .map
仅应在您想从旧数组构造新数组时使用,但您的代码没有这样做 - 您只是在执行副作用 - form.active
和listofferPrice
的设置。
The first step would be to use forEach
or for..of
instead, eg:第一步是使用
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
}
}
}
But since it looks like you're trying to find a possible single matching value in the array, .find
would be more appropriate:但由于看起来您正试图在数组中找到一个可能的单个匹配值,因此
.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.