繁体   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