What is the omit() function in Underscore.js?
Overview
With the omit() method of Underscore.js, we can return an object's properties by omitting some specified ones. We only need to specify the properties we want to omit.
Syntax
_.omit(object, *property)
Parameters
object: This is the object we want to return but omit some of its properties.
*property: This is a property we want to omit. It could be one or more.
Return value
The value returned is an object with some of its properties omitted.
Example
// require underscoreconst _ = require("underscore")// create some objectslet obj1 = {one: 1, two: 2, three: 3, four: 4}let obj2 = {name: "nodejs", package: "underscore", tag: "_"}let obj3 = {a: "Apple", b: "Banana"}let obj4 = {M: "Monday", T: "Tuesday", W: "Wednesday"}// get inverted objectsconsole.log(_.omit(obj1, "one"))console.log(_.omit(obj2, "package", "tag"))console.log(_.omit(obj3, "a", "b"))console.log(_.omit(obj4, "T", "M"))
Explanation
- Line 2: We require the
underscorepackage. - Line 5–8: We create some objects.
- Line 11–14: We use the
omit()method to omit some properties of the objects we created. Then, we print the results to the console.