1

I want to assign a new Value (let say this is a flag such as isActive) from an existing object. this is an example for the object I'm using to try:

let localValue = {
  "id": "019eadd3-2e71-4446-a195-69d849d88a43",
  "discount": {
      "code": "PFMQWERTY",
      "id": "019eadd3-2e71-4446-a195-69d849d88a43",
      "isActive": false
  },
  "discountId": "019eadd3-2e71-4446-a195-69d849d88a43",
  "discountRules": [
    {
      "id": "1-1",
      "type": "FIXED",
      "value": 30000,
      "package": {
        "id": "1-1-A",
        "name": "Package A",
        "maxDiscountInApps": 3,
        "discountInApps": [
          {
            "id": "1-1-A-A",
            "code": "QWERTY",
            "expirationDate": "2034-02-28T00:00:00+0000"
          }
        ]
      }
    },
    {
      "id": "1-2",
      "type": "FIXED",
      "value": 100000,
      "package": {
        "id": "1-2-A",
        "name": "Package B",
        "maxDiscountInApps": 3,
        "discountInApps": [
          {
            "id": "1-2-A-A",
            "code": "KASH",
            "expirationDate": "2032-02-03T00:00:00+0000"
          }
        ]
      }
    },
    {
      "id": "1-3",
      "type": "FIXED",
      "value": 15000,
      "package": {
        "id": "1-3-A",
        "name": "Package C",
        "maxDiscountInApps": 3,
        "discountInApps": []
      }
    },
    {
      "id": "1-4",
      "type": "FIXED",
      "value": 180000,
      "package": {
        "id": "1-4-A",
        "name": "Package D",
        "maxDiscountInApps": 3,
        "discountInApps": []
      }
    },
    {
      "id": "1-5",
      "type": "FIXED",
      "value": 15000,
      "package": {
        "id": "1-5-A",
        "name": "",
        "maxDiscountInApps": 3,
        "discountInApps": []
      }
    },
    {
      "id": "1-6",
      "type": "FIXED",
      "value": 30003,
      "package": {
        "id": "1-6-A",
        "name": "Package E",
        "maxDiscountInApps": 3,
        "discountInApps": [
          {
            "id": "1-6-A-A",
            "code": "QWERTY",
            "expirationDate": "2034-02-28T00:00:00+0000"
          },
          {
            "id": "1-6-A-B",
            "code": "KASH",
            "expirationDate": "2032-02-03T00:00:00+0000"
          },
          {
            "id": "1-6-A-C",
            "code": "ANT",
            "expirationDate": "2021-07-30T00:00:00+0000"
          }
        ]
      }
    },
    {
      "id": "1-7",
      "type": "FIXED",
      "value": 5000,
      "package": {
        "id": "1-7-A",
        "name": "Package F",
        "maxDiscountInApps": 3,
        "discountInApps": []
      }
    }
  ],
  "expirationDate": "28/02/2034 07:00:00",
  "totalPackagesShown": 2
}

the goals I want to achieve is to check if there is the same code in discountRules.package.discountInApps.code === this.localValue.discount.code then return true, if failed then return false.

I can already find the way to set it using map() and some() like this:

this.localValue.discountRules = this.localValue.discountRules.map(
  rule => ({
    ...rule,
    isActive:
      rule.package &&
      rule.package.discountInApps &&
      rule.package.discountInApps.length !== 0
        ? rule.package.discountInApps.some(
            ruleItems =>
              ruleItems.code === this.localValue.discount.code
          )
        : false
  })
);

but performance wise, is it better using map() and reducer() combination for this case or better stay using map() and some()? because I read it in here Array.prototype.reduce() it seems when using reducer can make an array reduce until it finds a value that I want (is my understanding correct?) so if it's true then...

is it better using this map() and reducer() (if its possible) or stay with map() and some() combination? (I'm still failed to implement map() and reducer(), so can someone tell me how to use this (map() and reducer()) combination?)

Notes: if by any chance there is a better way, I'm open for it

just in case, I'm already try to read this thread but still not quite understand as how to implement it in my case:

  1. remove-object-from-array-using-javascript
Rakis Friski
  • 551
  • 1
  • 7
  • 26
  • `discountRules.package.discountInApps.code === this.localValue.discount.code` then `return true` if you just want one Boolean then `.some()` is perfect. – zer00ne Feb 11 '22 at 18:07
  • @zer00ne yup it seems for this case as long as the object variable and array not too dynamic, it is better using this combination to get the best result, thankyou for the comment – Rakis Friski Feb 14 '22 at 02:54

1 Answers1

2

If you prefer performance, the traditional for loop would be the fastest.

For execution times, I get these

Map & Some: 27ms average
Map & Reduce: 32ms average

You can use console.time() and console.timeEnd() to check for execution times.

The array Reduce method will run a callback function on each element of the supplied array and will return the calculated value from the callback (the value will be stored in the index of the element). If you want to use reduce with map, you can do something like this

localValue.discountRules = localValue.discountRules.map((rule) => ({
  ...rule,
  isActive:
    rule.package &&
    rule.package.discountInApps &&
    rule.package.discountInApps.length !== 0
      ? rule.package.discountInApps.reduce(
          (accumulatedValue, currentValue) =>
            accumulatedValue && currentValue.code === localValue.discount.code,
          true // this is the default initial value for the accumulatedValue
        )
      : false,
}));

Alternatively, you can use optional chaining (?.) Like this

localValue.discountRules = localValue.discountRules.map((rule) => ({
  ...rule,
  isActive:
    rule?.package?.discountInApps?.some(
        (ruleItems) => ruleItems.code === localValue.discount.code
    )
}));

(Do check out the supported browsers here).

Huzaifa
  • 345
  • 4
  • 15
  • Thankyou for your answer, I'm quite confused as how to implement reduce, it seems using `some()` is better for this case since the depth of the object and array not to dynamic but when it need to dynamically search the value then `reduce()` is a better options, thankyou for the explanation – Rakis Friski Feb 14 '22 at 02:53