Skip to content
Advertisement

Find by key deep in a nested array

Let’s say I have an object:

JavaScript

I want to find the one object where the id is 1. Is there a function for something like this? I could use Underscore’s _.filter method, but I would have to start at the top and filter down.

Advertisement

Answer

Recursion is your friend. I updated the function to account for property arrays:

JavaScript

updated jsFiddle: http://jsfiddle.net/FM3qu/7/

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