Lodash _.isNumeric() 方法
Lodash _.isNumeric() 方法检查给定值是否为数值并返回相应的布尔值。它可以是包含数值、指数符号或 Number 对象等的字符串。
句法:
_.isNumeric( value );
参数:此方法采用单个参数,如上所述,如下所述:
- value:要检查数值的给定值。
返回值:如果给定值是数字,则此方法返回布尔值 true,否则返回 false。
注意:这在普通 JavaScript 中不起作用,因为它需要安装 lodash.js contrib 库。 Lodash.js contrib 库可以使用npm install lodash-contrib 安装。
示例 1:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
// Checking for _.isNumeric() method
console.log("The Value is Numeric : " + _.isNumeric(10000));
console.log("The Value is Negative : " + _.isNumeric(10.5));
console.log("The Value is Negative : " + _.isNumeric('G'));
console.log("The Value is Negative : " + _.isNumeric('Geeks'));
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
// Checking for _.isNumeric() method
console.log("The Value is Numeric : " + _.isNumeric([1,10]));
console.log("The Value is Negative : " + _.isNumeric("500"));
console.log("The Value is Negative : " + _.isNumeric({}));
console.log("The Value is Negative : " + _.isNumeric(null));
输出:
The Value is Numeric : true
The Value is Negative : true
The Value is Negative : false
The Value is Negative : false
示例 2:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
// Checking for _.isNumeric() method
console.log("The Value is Numeric : " + _.isNumeric([1,10]));
console.log("The Value is Negative : " + _.isNumeric("500"));
console.log("The Value is Negative : " + _.isNumeric({}));
console.log("The Value is Negative : " + _.isNumeric(null));
输出:
The Value is Numeric : false
The Value is Negative : true
The Value is Negative : false
The Value is Negative : false