最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use Ramda to find matching object in Array by key value - Stack Overflow

programmeradmin2浏览0评论

Ramda REPL example

var portfolio = [{ticker: "aa"},  {ticker: "bb"}];

var ticker = {ticker:"aa"};

var exist = R.find(R.propEq('ticker', ticker), portfolio)

console.log(exist)

Currently this is giving me undefined, however R.propEq should find the matching object by key ticker in port I thought?

Ramda REPL example

var portfolio = [{ticker: "aa"},  {ticker: "bb"}];

var ticker = {ticker:"aa"};

var exist = R.find(R.propEq('ticker', ticker), portfolio)

console.log(exist)

Currently this is giving me undefined, however R.propEq should find the matching object by key ticker in port I thought?

Share Improve this question asked Dec 27, 2016 at 20:06 Leon GabanLeon Gaban 39k122 gold badges348 silver badges549 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 18

As you say, you can solve it by passing in the key to propEq:

R.find(R.propEq('ticker', 'aa'), port)

Another option is to use the eqProps function, which tests if two objects match for the named key:

R.find(R.eqProps('ticker', ticker), port)

You can see the first or second version in the Ramda REPL.

Ah it was a simple mistake, I forgot to pass in the exact key from the ticker object.

R.propEq('ticker', ticker.ticker)

This is how I now solve my problem in my app:

const exists = R.find(R.propEq('ticker', this.ticker.ticker));
this.inPortfolio = !!exists(portTickers);
console.log('this.inPortfolio', this.inPortfolio)
// True or false
发布评论

评论列表(0)

  1. 暂无评论