How to use the pick function from underscore

Find comprehensive JavaScript underscore.pick code examples handpicked from public code repositorys.

underscore.pick is a function in the Underscore.js library that returns a new object with only the specified properties from the original object.

10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
    var keys = _.map(flatten(arguments, false, false, 1), String);
    iteratee = function(value, key) {
      return !_.contains(keys, key);
    };
  }
  return _.pick(obj, iteratee, context);
};

// Fill in a given object with default properties.
_.defaults = createAssigner(_.allKeys, true);
fork icon1
star icon1
watch icon2

+ 9 other calls in file

732
733
734
735
736
737
738
739
740
741
742
		}
	};
}


function getEnvVars() {
	const envVars = pick(process.env, `MP_PROJECT`, `MP_ACCT`, `MP_PASS`, `MP_SECRET`, `MP_TOKEN`, `MP_TYPE`, `MP_TABLE_ID`, `MP_GROUP_KEY`, `MP_START`, `MP_END`);
	const envKeyNames = {
		MP_PROJECT: "project",
		MP_ACCT: "acct",
		MP_PASS: "pass",
fork icon0
star icon13
watch icon6

How does underscore.pick work?

underscore.pick is a function in the Underscore.js library that creates a new object from an existing object, containing only the specified properties.

When underscore.pick is called with an object and a list of property names, it performs the following steps:

  1. It creates a new empty object.
  2. It iterates over the property names in the list.
  3. For each property name, it checks if the original object has a property with that name.
  4. If the original object has a property with that name, it adds the property to the new object.
  5. It returns the new object.

Here's an example of how you could use underscore.pick to create a new object with only the specified properties:

javascript
const originalObject = { name: 'Alice', age: 30, gender: 'female' }; const newObject = _.pick(originalObject, ['name', 'age']); console.log(newObject); // Output: { name: 'Alice', age: 30 }

In this example, we're creating a new object called originalObject with the properties name, age, and gender.

We're then using underscore.pick to create a new object called newObject that only contains the name and age properties from the original object.

When we log the newObject to the console, we see that it contains only the name and age properties:

css
{ name: 'Alice', age: 30 }

By using underscore.pick, you can easily create new objects from existing objects, containing only the properties that you're interested in.

926
927
928
929
930
931
932
933
934
935
}
const validPaths = _.pickSchema(
    mongoose.model("Profile"),
    ModelUtils.nonCloneable
);
updates = _.pick(updates, validPaths);
profile = _.extend(profile, updates);
try {
    const updatedProfile = await profile.save();
    return resolve(updatedProfile);
fork icon0
star icon2
watch icon0

215
216
217
218
219
220
221
222
223
224
    watches: 0,
    ownerName: "",
};
newRepo = new repoModel();
const cloneablePaths = _.pickSchema(repoModel, Utils.nonCloneable);
const sourceExtension = _.pick(clone(sourceRepo), cloneablePaths);
newRepo = _.extend(newRepo, sourceExtension);
newRepo = _.extend(newRepo, newExtension);
_.each(
    [
fork icon0
star icon2
watch icon0

+ 15 other calls in file

Ai Example

1
2
3
4
5
6
7
8
const _ = require("underscore");

const originalObject = { name: "Alice", age: 30, gender: "female" };

const newObject = _.pick(originalObject, ["name", "age"]);

console.log(newObject);
// Output: { name: 'Alice', age: 30 }

In this example, we're using underscore.pick to create a new object called newObject that only contains the name and age properties from the originalObject. When we run this code, we'll see that the newObject contains only the name and age properties: css Copy code

25
26
27
28
29
30
31
32
33
34

newTeam.description = "";

newTeam = _.extend(
    newTeam,
    _.pick(_.omit(team, Utils.nonCloneable), _.pickSchema(teamModel))
);

if (typeof team.prefix !== "string" || !team.prefix.length) {
    newTeam.prefix = newTeam.name;
fork icon0
star icon2
watch icon0

29
30
31
32
33
34
35
36
37
38

newProject.description = "";

newProject = _.extend(
    newProject,
    _.pick(_.omit(project, Utils.nonCloneable), _.pickSchema(projectModel))
);

if (typeof project.prefix !== "string" || !project.prefix.length) {
    newProject.prefix = newProject.name;
fork icon0
star icon2
watch icon0

78
79
80
81
82
83
84
85
86
87
}

async copyEntity(entity, changes = {}) {
    const excluded = ['_id', '__v', 'updated_at'];//, 'created_at', 'updated_at'];
    const attributesNames = this.pickSchema(excluded);
    const copiedEntity = _.pick(entity, attributesNames);

    const changedAttributesNames = _.keys(changes);
    const isChanged = changedAttributeName => changedAttributesNames.indexOf(changedAttributeName) > 0;
    const attributeExists = attributeName => attributesNames.indexOf(attributeName) >= 0;
fork icon0
star icon0
watch icon0

311
312
313
314
315
316
317
318
319
320
    advanceDeclineRatio,
};
const tickerRecs = {
    ...tickerRecData,
    timestamp: Date.now(),
    rawTickerRecs: mapObject(rawTickerRecs, recs => recs.map(rec => pick(rec, ['ticker', 'scan']))),
};
this.tickerRecs = tickerRecs;
this.sendToAll('server:data-update', {
    tickerRecs,
fork icon0
star icon0
watch icon0

50
51
52
53
54
55
56
57
58
59
};

await log(`accessing collections for based on run scan`);
const collections = await RealtimeRunner.refreshCollections();

const collectionScans = pick(collections, ['restOS', 'rhtop100', 'tickerRecTickers']);

// strlog({ collectionScans})

await log(`received collections for based on run scan: ${Object.keys(collections).length}`);
fork icon0
star icon0
watch icon0

60
61
62
63
64
65
66
67
68
69
initialize: function () {
  if (this.options.model) {
    this.model = this.options.model;
  } else {
    var modelAttrKeys = TweetBox.supportedAttributes();
    var modelAttrs = Underscore.pick(this.options, modelAttrKeys);
    this.model = new TweetBox(modelAttrs);
  }

  $.extend(this, PeacockRTLView(this));
fork icon0
star icon0
watch icon1

+ 2 other calls in file

1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
//  p.tickerRecsMyScore < 37,
const badThingsInWords = toWords(badThings);
const { recent500TrendPerDay, capDivFloat } = (p.scan || {}).computed || {};
// const bbScore = (p.stSent || {}).bullBearScore;
const positiveInflationCounts = mapObject(
  pick({
    wordScore: Number(p.wordScore > 0 && Math.round(p.wordScore / 0.4)),
    // wordNumber: Number(p.wordNumber > 2 && p.wordNumber / 10),
    recent500TrendPerDay: Number(
      recent500TrendPerDay < -3 && recent500TrendPerDay / -3
fork icon0
star icon0
watch icon1

+ 5 other calls in file