[英]How to use Ramda to find matching object in Array by key value
Ramda REPL example Ramda REPL的例子
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? 目前这给了我未定义的 ,但是R.propEq应该通过我认为的
port
的关键ticker
找到匹配的对象?
As you say, you can solve it by passing in the key to propEq
: 如您所说,您可以通过将键传递给
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: 另一种选择是使用
eqProps
函数,该函数测试两个对象是否与命名键匹配:
R.find(R.eqProps('ticker', ticker), port)
You can see the first or second version in the Ramda REPL. 您可以在Ramda REPL中看到第一个或第二个版本。
Ah it was a simple mistake, I forgot to pass in the exact key from the ticker object. 啊这是一个简单的错误,我忘了从ticker对象传递确切的密钥。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.