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

1 year ago
1

Hi Friends,

method #2 `_.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 number property within the nested street object. The result is 123.

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 streetNumber = _.get(person, ["address", "street", "number"]);
console.log(streetNumber);
// 123

#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...