Underscore.js is a library used to manipulate arrays and objects in JavaScript. With the pick()
method, we can filter the properties of an object and return only some specified ones.
_.pick(object, *property)
object
: This is the object whose properties we want to filter.*property
: This is a property we want to return. It can be one or more.This function returns an object with only some properties specified.
// 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"}// pick or select some propertiesconsole.log(_.pick(obj1, "one"))console.log(_.pick(obj2, "package", "tag"))console.log(_.pick(obj3, "a", "b"))console.log(_.pick(obj4, "T", "M"))
underscore
package. obj1
, obj2
, obj3
, and obj4
. We give them some properties and values.pick()
method to filter "one"
property of the object obj1
and log the result to the console.pick()
method to select the properties, "package"
and "tag"
, of obj2
and print the result to the console.pick()
method to filter the properties, "a"
and "b"
, of obj4
and print the result to the console."T"
and "M"
, of obj4
and log the result to the console.