Skip to content
Advertisement

Lodash – Find deep in array of object

I have an array of an object like this

JavaScript

What I need is sum the property Val when prop is foo. So, I have to search through elements and get all objects where prop is foo. With this objects, I should sum the val property.

I tried to use many combinations of _.find, _.pick and so on, but I don’t get the right result. Can someone help me?

Advertisement

Answer

Here’s a solution that flattens the elements and then filters the result to get the required elements before summing the val property:

JavaScript

Chaining is a way of applying a sequence of operations to some data before returning a value. The above example uses explicit chaining but could be (maybe should be) written using implicit chaining:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement