📜  Lodash _.some() 方法

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

Lodash _.some() 方法

Lodash 是一个基于 underscore.js 的 JavaScript 库。 Lodash 有助于处理数组、集合、字符串、对象、数字等。

_.some() 方法用于检查谓词是否对集合的任何元素返回 true。一旦谓词返回 true,迭代就会停止。

句法:

_.some(collection, predicate)

参数:此方法接受上面提到的两个参数,如下所述:

  • 集合:此参数保存要迭代的集合。
  • 谓词:此参数保存每次迭代调用的函数,并使用三个参数(值、索引|键、集合)调用。

返回值:如果任何元素通过谓词检查,则此方法用于返回true,否则返回false。

示例一:这里使用 const _ = require('lodash') 来导入文件中的 lodash 库。

Javascript
// Requiring the lodash library
const _ = require("lodash");
      
// Original array and use of _.some() method
var gfg = _.some([null, 0, 'yes', false], Boolean);
 
// Printing the output
console.log(gfg);


Javascript
// Requiring the lodash library
const _ = require("lodash");
      
// Original array
var object = [
  { 'obj': 'moto', 'active': true },
  { 'obj': 'lenovo',   'active': false } ];
  
// Use of _.some() method
// The `_.matches` iteratee shorthand
 
let gfg = _.some(object, { 'obj': 'moto', 'active': false });
 
// Printing the output
console.log(gfg);


Javascript
// Requiring the lodash library
const _ = require("lodash");
      
// Original array
 
var object = [
  { 'obj': 'moto', 'active': true },
  { 'obj': 'lenovo',   'active': false } ];
  
// Use of _.some() method
// The `_.matchesProperty` iteratee shorthand
 
let gfg = _.some(object, ['active', false]);
 
// Printing the output
console.log(gfg);


Javascript
// Requiring the lodash library
const _ = require("lodash");
      
// Original array
var object = [
  { 'obj': 'moto', 'active': true },
  { 'obj': 'lenovo',   'active': false } ];
  
// Use of _.some() method
// The `_.property` iteratee shorthand
 
let gfg = _.some(object, 'active');
 
// Printing the output
console.log(gfg);



输出:

true

示例 2:

Javascript

// Requiring the lodash library
const _ = require("lodash");
      
// Original array
var object = [
  { 'obj': 'moto', 'active': true },
  { 'obj': 'lenovo',   'active': false } ];
  
// Use of _.some() method
// The `_.matches` iteratee shorthand
 
let gfg = _.some(object, { 'obj': 'moto', 'active': false });
 
// Printing the output
console.log(gfg);


输出:

false

示例 3:

Javascript

// Requiring the lodash library
const _ = require("lodash");
      
// Original array
 
var object = [
  { 'obj': 'moto', 'active': true },
  { 'obj': 'lenovo',   'active': false } ];
  
// Use of _.some() method
// The `_.matchesProperty` iteratee shorthand
 
let gfg = _.some(object, ['active', false]);
 
// Printing the output
console.log(gfg);


输出:

true

示例 4:

Javascript

// Requiring the lodash library
const _ = require("lodash");
      
// Original array
var object = [
  { 'obj': 'moto', 'active': true },
  { 'obj': 'lenovo',   'active': false } ];
  
// Use of _.some() method
// The `_.property` iteratee shorthand
 
let gfg = _.some(object, 'active');
 
// Printing the output
console.log(gfg);


输出:

true

注意:此代码在普通 JavaScript 中不起作用,因为它需要安装库 lodash。