How to use the contains function from lodash

Find comprehensive JavaScript lodash.contains code examples handpicked from public code repositorys.

lodash.contains has been deprecated in favor of lodash.includes and returns a boolean value indicating if a given value is present in an array or string.

3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
* @param {*} target The value to check for.
* @param {number} [fromIndex=0] The index to search from.
* @returns {boolean} Returns `true` if the `target` element is found, else `false`.
* @example
*
* _.contains([1, 2, 3], 1);
* // => true
*
* _.contains([1, 2, 3], 1, 2);
* // => false
fork icon73
star icon711
watch icon29

+ 7 other calls in file

81
82
83
84
85
86
87
88
89
90
    return _.size(this.infos.scripts || {}) > 0;
};

// Check if the addon is blacklisted
this.isBlacklisted = function() {
    return _.contains(this.options.blacklist, this.infos.name);
};

// Optimize the addon
this.optimizeClient = function(force) {
fork icon13
star icon24
watch icon6

+ 5 other calls in file

How does lodash.contains work?

lodash.contains is an old method that has been deprecated in favor of lodash.includes. lodash.includes checks if a given value is present in an array or string and returns a boolean. It works by using strict equality (===) to compare the value with each element in the array (or character in the string).

444
445
446
447
448
449
450
451
452
453
//
// ### Spawn Casper.js
// Custom test runner for our Casper.js functional tests
// This really ought to be refactored into a separate grunt task module
grunt.registerTask('spawnCasperJS', function (target) {
    target = _.contains(['client', 'setup'], target) ? target + '/' : undefined;

    var done = this.async(),
        options = ['host', 'noPort', 'port', 'email', 'password'],
        args = ['test']
fork icon0
star icon1
watch icon2

+ 3 other calls in file

378
379
380
381
382
383
384
385
386
387

// Unless `all` is passed as an option, filter on
// the status provided.
if (options.status !== 'all') {
    // make sure that status is valid
    options.status = _.contains(['published', 'draft'], options.status) ? options.status : 'published';
    options.where.status = options.status;
}

return options;
fork icon0
star icon1
watch icon2

+ 9 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
const _ = require("lodash");

const numbers = [1, 2, 3, 4, 5];

console.log(_.includes(numbers, 3)); // true
console.log(_.includes(numbers, 6)); // false

const sentence = "The quick brown fox jumps over the lazy dog";

console.log(_.includes(sentence, "fox")); // true
console.log(_.includes(sentence, "cat")); // false

In this example, we first require the lodash library and define an array of numbers and a sentence. We then use _.includes to check if the number 3 is present in the numbers array and if the word 'fox' is present in the sentence. We also check if the number 6 and the word 'cat' are present in the array and the string, respectively. The method returns true if the value is present and false if it's not.

51
52
53
54
55
56
57
58
59
60
        model.emitChange('activated');
    }
});
this.on('updated', function onUpdated(model) {
    model.statusChanging = model.get('status') !== model.updated('status');
    model.isActive = _.contains(activeStates, model.get('status'));

    if (model.statusChanging) {
        model.emitChange(model.isActive ? 'activated' : 'deactivated');
    } else {
fork icon0
star icon1
watch icon2

66
67
68
69
70
71
72
73
74
  var evaluations = _.chain(mdResults).map('evaluated').flatten().value()
  var evalResults = _.chain(evaluations).map('evalResult').flatten().value()
  var evalResultsInstanceofError = _.map(evalResults, function (evalResult) {
    return evalResult instanceof Error
  })
  var evalResultsHasInstanceofError = _.contains(evalResultsInstanceofError, true)
  if (evalResultsHasInstanceofError) return 1
  return 0
}
fork icon0
star icon1
watch icon2

+ 3 other calls in file

84
85
86
87
88
89
90
91
92
93
var followingNodesOnPage = _.chain(followingNodeList).drop(index + 1).filter(function (node0) {
      return _.contains(node0.nodeInfo.pageNumbers, pageNumber);
    }).value();

    var nodesOnNextPage = _.chain(followingNodeList).drop(index + 1).filter(function (node0) {
      return _.contains(node0.nodeInfo.pageNumbers, pageNumber + 1);
    }).value();

    var previousNodesOnPage = _.chain(followingNodeList).take(index).filter(function (node0) {
      return _.contains(node0.nodeInfo.pageNumbers, pageNumber);
fork icon0
star icon0
watch icon1

+ 17 other calls in file

34
35
36
37
38
39
40
41
42
43

  if (arr.length < 2) {
    return false;
  }

  return _.contains(fileTypes, arr[1]);
}

// create proxy to couch for all couch requests
var proxy = httpProxy.createServer({
fork icon0
star icon0
watch icon0

301
302
303
304
305
306
307
308
309
  helpers._utxos = utxos;
}

blockchainExplorer.getUtxos = function(addresses, cb) {
  var selected = _.filter(helpers._utxos, function(utxo) {
    return _.contains(addresses, utxo.address);
  });
  return cb(null, selected);
};
fork icon0
star icon0
watch icon0

+ 3 other calls in file

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)