bier hier bier hier - 13 days ago 5
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

read lodash documentation and watch examples

_.find

Arguments

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'])