Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

underscore.js

What is the omit() function in Underscore.js?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 underscore
const _ = require("underscore")
// create some objects
let 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 objects
console.log(_.omit(obj1, "one"))
console.log(_.omit(obj2, "package", "tag"))
console.log(_.omit(obj3, "a", "b"))
console.log(_.omit(obj4, "T", "M"))
The omit() method of Underscore.js

Explanation

  • Line 2: We require the underscore package.
  • 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.

RELATED TAGS

underscore.js

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring