Grammar:
_.contains (list, item, FromIndex, guard)
Description
Returns true if the list collection contains the specified value, otherwise false
- List can be arrays, objects, strings, and arguments
- Item is a parameter (contains will handle whether the list contains this parameter), it can be a number, a string
- Fromindex is a number that determines the position at which the list starts to be retrieved, either positive or negative, (negative values are not true negative values, but the countdown to the list, which is explained in detail below); Fromindex Index starting with 0 if the number is not.
Code example: Example One: Contains retrieves a list based on item and returns TRUE if it contains Item
varresult;//Array to retrieveresult = _.contains ([1, 2, 3], 2); Console.log (Result)//= True//Object for Retrievalresult = _.contains ({x:1, y:2, Z:3}, 2); Console.log (Result)//= True//string to retrieveresult = _.contains (' 123 ', "2"); Console.log (Result)//= True//Arguments for RetrievalfunctionABC () {Result= _.contains (arguments, 2); Console.log (result); //= True}ABC (1, 2, 3);
Example two: Item property
var result; // item is numeric result = _.contains ([' 1 ', 2, ' 3 '], 2 // => true Span style= "COLOR: #008000" >// item is string result = _.contains ({x: ' 1 ', y: ' 2 ', Z: ' 3 '}, "4" // => false // Item is bool result = _.contains ([0, false ], True // Span style= "COLOR: #008000" >=> false
Example three: Fromindex index value (can be positive or negative, non-number type Fromindex default is 0)
varresult;//item is a numberresult = _.contains ([1, 2, 3], 3, 0); Console.log (Result)//= True//item is a stringresult = _.contains ([1, 2, 3], 3, "3"); Console.log (Result)//= True//Item is negativeresult = _.contains ({x: ' 1 ', y: ' 2 ', Z: ' 3 '}, "3", 1); Console.log (Result)//= Trueresult= _.contains ({x: ' 1 ', y: ' 2 ', Z: ' 3 '}, "2", 1); Console.log (Result)//= = False It can be seen that when Fromindex is negative, Fromindex is based on the last countdown of the list
Contains can also have a fourth parameter guard; Example one: Guard is a truth-case Fromindex index starting from 0; (this attribute is not clearly stated by the official, but it is personal understanding, if there is better advice, you can contact the author)
varresult;//Guard is empty, it is retrieved according to the Fromindex indexresult = _.contains ([' A ', ' B ', ' C '], ' C ', 2); Console.log (Result)//= True//Guard is trueresult = _.contains ([' A ', ' B ', ' C '], ' C ', 3,true); Console.log (Result)//= True//Guard is falseresult = _.contains ([' A ', ' B ', ' C '], ' C ', 3,false); Console.log (Result)//= False
Underscorejs _.contains (list, item, FromIndex, guard)