0

I have bunch of objects in an array with format as below:

[
  {
    "card_details": {
      "cardType": "gift"
    },
    "merchant_details": {
      "merchantName": "Walter Heisenberg1",
      "timeZone": "+05:30",
    }
  },
  {
    "card_details": {
      "cardType": "coupon",
      "cardTitle": "Coupon",
      "messageUser": "Hi",
      "punchCount": null,
      "messageReachPunchLimit": "Get your Freebie!",
      "merchantId": "59c214000e1a7825184cb813",
      "expiryDate": "21 Sep 2019",
      "discountPercent": "15",
      "cardImageUrl": ""
    },
    "merchant_details": {
      "merchantName": "Walter Heisenberg1",
      "timeZone": "+05:30"
    }
  },
  {
    "card_details": {
      "cardType": "punch",
      "cardTitle": "BlueM2",
      "messageUser": "Welcome!",
      "expiryDate": "21 Sep 2019",
      "punchCount": 25,
      "messageReachPunchLimit": "Get your Freebie!",
      "merchantId": "59c214000e1a7825184cb813",
      "cardImageUrl": "http://139.59.179.111/cloopapi/undefined"
    },
    "merchant_details": {
      "merchantName": "Walter Heisenberg1",
      "timeZone": "+05:30"
    }
  }
]

I want to filter the objects based on cardType in the card_details object. But I want to search from the array. For example, if I search for ["coupon","gift"], then I should get the all the cards which have the card_details.cardType as coupon or gift. I need to be able to do this in Node.js,i.e., Javascript.

0

1 Answer 1

2

You can use ES6 filter and includes functions here:

collection.filter(item => ['coupon', 'gift'].includes(item.card_details.cardType))
Sign up to request clarification or add additional context in comments.

1 Comment

Is there any way to do this in ES5?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.