dimanche 5 avril 2020

How do I add an if , else if, else condition to my arrow function which includes .map and .filter already?

Below is a function that needs to set the option.value of a selected element inside the options array. It needs to set it to $1.50 each if 3 or more options selected, and if 6 or more are selected it needs to be 1.00 each. Right now it works like a charm as it simply takes the options.value from the array and sets it to it's current price, however I need to add this discount functionality to my function. How can I achieve this?

My current working function without discount condition.

private getSelectedOptions(options) {
      return (options || []).filter(x => x.selected).map
         (option => ({ name: option.name, value: option.value || 0 }));
 }

Options array

 "options": [
    {
      "name": "Red Pepper",
      "selected": false,
      "value": 2.00
    },
    {
      "name": "Garlic",
      "selected": false,
       "value": 2.00

    },
    {
      "name": "Cheese blend",
      "selected": false,
       "value": 2.00
    },
     {
      "name": "pineapple",
      "selected": false,
       "value": 2.00
    },
     {
      "name": "bacon",
      "selected": false,
       "value": 2.00
    },
    {
      "name": "green pepper",
      "selected": false,

    }

Aucun commentaire:

Enregistrer un commentaire