0

I'm using this method:

processPromotions(){
    let promotionWithItem;
    this.allPromotions = this.sellerPromotions.map(promotion =>{
         promotionWithItem = this.findPromotion(promotion, false);
        if(promotionWithItem != undefined){
            promotion.item_in_promotion = true;
            promotion.item_regular_price = promotionWithItem.regular_price;
            promotion.item_deal_price = promotionWithItem.deal_price;
        }else{
            promotion.item_in_promotion = false;
        }
    })
    console.log(JSON.stringify(this.allPromotions[0]));
}

I'm mapping an existing array of objects to add new properties. Right now, I have one object in the original array, and when I checked the length of the new one, it says 1. but, when I log the object in the 0 index, it says undefined. Any idea why?

1 Answer 1

1

Except for the single line case of e.g.:

a => a + 100;

you do need to use return so adding:

return promotion;

at the end of your => function would likely solve your problem.

(Though as you are mutating the sellerPromotions items the code might have unexpected side effects too.)

See e.g. Arrow function expressions.

2
  • Thanks! I just talked to a coworker and he said exactly that, if I don't add a return, it wont do anything. and also, he said that it is not necessary to use the map function in this case, I can just assign the allPromotions array to the sellerPromotions and then use a for each to make the changes. Mar 5, 2021 at 19:40
  • That wording is a bit weird. It's more accurate to say "if you use a code block {}, then a return statement is necessary". It's possible to use "more than one line" without a return statement.
    – sfdcfox
    Mar 6, 2021 at 16:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .