I have a nested object like Now I want to get the level of the master parent object or basically how many times a parent object has been nested. In this case, I should be getting an output of 3. Answer You could also do it recursively like this:
Tag: underscore.js
How can I make Underscore behave like Ramda?
Two days ago, I announced a preview release of Underscore that integrates with the new Node.js way of natively supporting ES modules.1 Yesterday, somebody responded on Twitter with the following …
Filter Object Array 1 on the basis on 2nd Object Array in Javascript(UnderscoreJS)
I wanted to filter Object Array 1 if it’s object value not exist in 2nd object Array. Non-Intersected values from 2nd array But the result is using this code is wrong. please help me here Answer Here is a solution based on underscore. b.id !== a.group -> a.id !== b.group to match your objects’ structure. […]
Updating nested object by path in javascript
Let’s say I have this object: I’d like to be able to update the price in this object through a function call as follows: This answer Javascript: how to dynamically create nested objects using object names given by an array is good but does not address the case when there are arrays in the object. Underscore acceptable. Note: this answer
Add property to an array of objects
I have an array of objects as shown below Object {Results:Array[2]} Results:Array[2] [0-1] 0:Object id=1 name: “Rick” 1:Object id=2 name:’david’ I want to …
Get first element of a collection that matches iterator function
I would like to achieve something like _.first with _.filter, that is, having a collection of elements, I’d like to get the first one (if exists) that matches a truth test (iterator). For example, …
Convert returned JSON Object Properties to (lower first) camelCase
I have JSON returned from an API like so: To keep this consistent with my code style (camelCase – lower case first letter) I want to transform the array to produce the following: What’s the easiest/best way to do this? Create a new Contact object and iterate over all the contacts in the returned array? […]
Is there any way to rename js object keys using underscore.js
I need to convert a js object to another object for passing onto a server post where the names of the keys differ for example needs to turn into where I have lookup obj available for mapping all the keys Is there a function available in underscore.js or jQuery that I can use that does this functionality? thanks Answer As