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.

4
  • Why is this marked as duplicate and closed? He specifically asks for a solution using Ramda, and the link to the previous answer doesn't do that. Commented Sep 11, 2019 at 16:09
  • R.find(R.eqProps('GIFT_CARD', cardType), yourArrayOfObjects) Commented Sep 11, 2019 at 16:11
  • @bdbdbd updated with a ramda duplicate. Please @<username> to notify a user. Commented Sep 11, 2019 at 16:19
  • @adiga thanks, I didn't know that and thanks for updating Commented Sep 11, 2019 at 16:22

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.