bier hier bier hier - 1 year ago 90
React JSX Question

How can I find my object with lodash by id?

I have a userData object (which comes from a redux store) that contains this:

"packs": [
"name": "Docos",
"id": "101",
"price": 1000,
"gifted": false
"name": "Entertainment Plus",
"id": "102",
"price": 1000,
"gifted": false

I am trying to find the 1st one like this:

let pack = _.find(this.props.userData.packs, 'id', "101")

But when I run it it says pack is undefined? why?

Answer Source

read lodash documentation and watch examples



collection (Array|Object): The collection to inspect.

[predicate=_.identity] (Function): The function invoked per iteration.

[fromIndex=0] (number): The index to search from.

1 shorthand

_.find(this.props.userData.packs, {id: '101'})

2 shorthand

_.find(this.props.userData.packs, ['id', '101'])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download