Skip to content
Advertisement

Tag: underscore.js

Merge two objects and overwrite the values if conflict

I’m trying to merge two objects and overwrite the values in the process. Is it possible with underscore to do the following? (I’m fine with not using underscore I just want it to be simple) Answer You could use Underscore’s extend: The first argument is modified, so if you don’t want to modify obj1 or obj2 just pass in {}.

Find by key deep in a nested array

Let’s say I have an object: 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. Answer Recursion is your friend. I updated the function to account for property arrays: updated jsFiddle:

Advertisement