📜  Lodash _.includes() 方法

📅  最后修改于: 2022-05-13 01:56:20.301000             🧑  作者: Mango

Lodash _.includes() 方法

_.includes()方法用于查找值是否在集合中。如果集合是一个字符串,它将被测试一个值子字符串,否则 SameValueZero() 方法用于相等比较。如果给定索引并且为负数,则从集合的结束索引开始测试该值作为偏移量。

句法:

_.includes( collection, value, index )

参数:此方法接受三个参数,如上所述,如下所述:

  • 集合:此参数保存要检查的集合。集合可以是数组、对象或字符串。
  • value:此参数保存要搜索的值。
  • index:此参数保存要搜索的索引。

返回值:如果在集合中找到该值,则此方法返回true ,否则返回false

示例 1:

Javascript
// Requiring the lodash library  
const _ = require("lodash");  
  
// Collection of string
var name = [ 'gfg', 'geeks', 'computer', 'science', 'portal' ];
  
// Check value is found or not by _.includes() method
console.log(_.includes(name, 'computer'));
  
// Check value is found or not by _.includes() method
console.log(_.includes(name, 'geeeks')); 
  
// Check value is found or not by _.includes() method
console.log(_.includes(name, 'gfg', 2));


Javascript
// Requiring the lodash library  
const _ = require("lodash");  
  
// Collection of integer value
var name = [ 10, 15, 20, 25, 30 ]; 
  
// Check value is found or not by _.includes() method
console.log(_.includes(name, 25));
  
// Check value is found or not by _.includes() method
console.log(_.includes(name, 35));
  
// Check value is found or not by _.includes() method
console.log(_.includes(name, 25, 3));


输出:

true
false
false

示例 2:

Javascript

// Requiring the lodash library  
const _ = require("lodash");  
  
// Collection of integer value
var name = [ 10, 15, 20, 25, 30 ]; 
  
// Check value is found or not by _.includes() method
console.log(_.includes(name, 25));
  
// Check value is found or not by _.includes() method
console.log(_.includes(name, 35));
  
// Check value is found or not by _.includes() method
console.log(_.includes(name, 25, 3));

输出:

true
false
true