Lodash _.exists() 方法
Lodash _.exists() 方法检查给定值是否存在并返回相应的布尔值。 null 和 undefined 都被认为是不存在的值。所有其他值都是“存在”。
句法:
_.exists( value );
参数:此方法接受上面提到的单个参数,如下所述:
- value:要检查 Existy 值的给定值。
返回值:如果给定值为 Existy,则此方法返回布尔值 true,否则返回 false。
注意:这在普通 JavaScript 中不起作用,因为它需要安装lidash.js contrib 库。 Lodash.js contrib 库可以使用npm install lodash-contrib 安装。
示例 1:
Javascript
// Defining underscore lodash variable
var _ = require('lodash-contrib');
var bool = _.exists("Geeks");
console.log("Check the Existenc of Given Value : ", bool);
Javascript
// Defining underscore lodash variable
var _ = require('lodash-contrib');
var bool = _.exists(undefined);
console.log("Check the Existenc of Given Value : ", bool);
Javascript
// Defining underscore lodash variable
var _ = require('lodash-contrib');
var bool = _.exists(null);
console.log("Check the Existenc of Given Value : ", bool);
Javascript
// Defining underscore lodash variable
var _ = require('lodash-contrib');
var bool = _.exists([1, 12, 2, 3]);
console.log("Check the Existenc of Given Value : ", bool);
输出:
Check the Existenc of Given Value : true
示例 2:
Javascript
// Defining underscore lodash variable
var _ = require('lodash-contrib');
var bool = _.exists(undefined);
console.log("Check the Existenc of Given Value : ", bool);
输出:
Check the Existenc of Given Value : false
示例 3:
Javascript
// Defining underscore lodash variable
var _ = require('lodash-contrib');
var bool = _.exists(null);
console.log("Check the Existenc of Given Value : ", bool);
输出:
Check the Existenc of Given Value : false
示例 4:
Javascript
// Defining underscore lodash variable
var _ = require('lodash-contrib');
var bool = _.exists([1, 12, 2, 3]);
console.log("Check the Existenc of Given Value : ", bool);
输出:
Check the Existenc of Given Value : true