Lodash _.truthy() 方法
Lodash _.truthy() 方法检查给定值是否为真,并返回相应的布尔值。真值是在布尔上下文中强制为真的值。
句法:
_.truthy( value );
参数:此方法接受上面提到的单个参数,如下所述:
- value:要检查真实值的给定值。
返回值:如果给定值为真,则此方法返回布尔值 true,否则返回 false。
注意:这在普通 JavaScript 中不起作用,因为它需要安装 lodash.js contrib 库。 Lodash.js contrib 库可以使用npm install lodash-contrib 安装。
示例 1:
Javascript
// Defining underscore lodash variable
var _ = require('lodash-contrib');
var bool = _.truthy(true);
console.log("Given Value is True or not : ", bool);
var bool = _.truthy(false);
console.log("Given Value is True or not : ", bool);
Javascript
// Defining underscore lodash variable
var _ = require('lodash-contrib');
var bool = _.truthy(10);
console.log("Given Value is True or not : ", bool);
var bool = _.truthy([10, 30, 50]);
console.log("Given Value is True or not : ", bool);
var bool = _.truthy({"G" : "GeeksforGeeks"});
console.log("Given Value is True or not : ", bool);
输出:
Given Value is True or not : true
Given Value is True or not : false
示例 2:
Javascript
// Defining underscore lodash variable
var _ = require('lodash-contrib');
var bool = _.truthy(10);
console.log("Given Value is True or not : ", bool);
var bool = _.truthy([10, 30, 50]);
console.log("Given Value is True or not : ", bool);
var bool = _.truthy({"G" : "GeeksforGeeks"});
console.log("Given Value is True or not : ", bool);
输出:
Given Value is True or not : true
Given Value is True or not : true
Given Value is True or not : true