简体   繁体   中英

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:

Expected to return a value in arrow function array-callback-return

You are using .map incorrectly . .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 .

The first step would be to use forEach or for..of instead, eg:

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:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM