`_.get` function on object using JavaScript Library underscore.js ( #shorts )

1 year ago
1

Hi Friends,

`_.get` function on object using JavaScript Library underscore.js

`_.get` is a utility function in Underscore.js library is commonly used to retrieve the value of a nested property within an object, even if some intermediate properties do not exist without facing errors due to missing or undefined intermediate properties.

In below example, we have an object person with nested properties representing a person's details. We use _.get to retrieve the value of the city property within the address object. The result is "New York".

Note: _.get helps you handle situations where intermediate properties may be missing or undefined, preventing errors and allowing you to provide fallback values if needed.

// syntax
_.get(object, path, [defaultValue])

// example
var person = { name: "Annie", address: {
city: "New York",
street: { number: 123, name: "Main Street" }
}
};

var cityName = _.get(person, "address.city");
console.log(cityName);
// "New York"

#javascript #under #score #underscore #underscorejs #get #underscoreget #getfunction #unique #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official

Loading comments...