How to check an element exists or not in an array in Underscore.js?

devquora
devquora

Posted On: Feb 22, 2018

 

_.contains method is used to check an element in array exists or not in Underscore.js.

Prototype : _.contains(list, value, [fromIndex])

It will take three arguments first two are required and third is optional. The internally _.contains method uses indexOf if the supposed list is a type of array.

Example

var arrayList=['aaa', 'bbb', 'cfp', 'ddd'];
var needle1="cfp";
var needle2="abc";
_.contains(arrayList, needle1); // outputs true
_.contains(arrayList, needle2); // outputs false
 

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    Underscore.js Interview Questions

    Describe Underscore.js is ?

    Underscore.js is a javascript library that provides helper functions to manipulate an object without extending code any ..

    Underscore.js Interview Questions

    Explain Collection Functions in Underscore.js.list some under collection functions in Underscore.js?

    Collection Functions are functions that work on arrays, objects, and array-like objects such as arguments, NodeList and..