2

I have a array of objects this way.

0:
cardLast4Digits: "0664"
cardType: "GIFT_CARD"
__proto__: Object
1:
cardLast4Digits: "5551"
cardType: "CREDIT_CARD"
__proto__: Object

I want to loop through this array of object and find if cardType is "GIFT_CARD". Once i find it, i want to get that object as a result. Output should then be

0:
cardLast4Digits: "0664"
cardType: "GIFT_CARD"
__proto__: Object

Can someone please suggest me how to do this withv ramda.

adiga
  • 34,372
  • 9
  • 61
  • 83
user3019647
  • 133
  • 3
  • 13

1 Answers1

1

Just use the array find method: https://ramdajs.com/docs/#find

Ramda:

const items = [{
  cardLast4Digits: '0664',
  cardType: 'GIFT_CARD'
}, {
  cardLast4Digits: '5551',
  cardType: 'CREDIT_CARD'
}];
R.find(R.propEq('cardType', 'GIFT_CARD'))(items);

ES6:

const items = [{
  cardLast4Digits: '0664',
  cardType: 'GIFT_CARD'
}, {
  cardLast4Digits: '5551',
  cardType: 'CREDIT_CARD'
}];

const result = items.find(item => item.cardType === 'GIFT_CARD');
console.log(result);
Jeff Wooden
  • 5,339
  • 2
  • 19
  • 24
  • That's still not using Ramda, but just the built in JS find method. `R.find(R.eqProps('cardType', 'GIFT_CARD'))(items)` is the Ramda way. – bdbdbd Sep 11 '19 at 16:18