简体   繁体   English

如何修复警告“预计在箭头 function 数组回调返回中返回一个值”

[英]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.activelistofferPrice的设置。

The first step would be to use forEach or for..of instead, eg:第一步是使用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
        }
    }
}

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.

相关问题 如何修复“预期在箭头函数数组回调返回中返回一个值”? - How to fix "Expected to return a value in arrow function array-callback-return"? 如何解决警告“预期在此函数array-callback-return中返回值” - How to fix warning “ Expected to return a value in this function array-callback-return ” 预期在箭头函数array-callback-return中返回一个值 - Expected to return a value in arrow function array-callback-return 预期在箭头函数结束时返回一个值:array-callback-return - Expected to return a value at the end of arrow function: array-callback-return 预期在箭头函数array-callback-return的末尾返回一个值 - Expected to return a value at the end of arrow function array-callback-return React:箭头函数需要返回值array-callback-return - React: Arrow function expected a return value array-callback-return 正确警告:“预期在箭头函数数组回调返回中返回一个值” - Correct warning : "Expected to return a value in arrow function array-callback-return " 反应警告 预期在箭头函数数组-回调-返回结束时返回一个值 - react warning Expected to return a value at the end of arrow function array-callback-return 如何解决这个警告警告 Array.prototype.map() 期望从箭头函数数组回调返回的返回值? - How fix this warrning warning Array.prototype.map() expects a return value from arrow function array-callback-return? 为什么我收到此警告 159:35 警告预计将在箭头 function 数组回调返回的末尾返回一个值? - Why i am getting this warning 159:35 warning Expected to return a value at the end of arrow function array-callback-return?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM