Lodash - has 方法

语法

_.has(object, path)

检查 path 是否是 object 的直接属性。

参数

  • object (Object) − 要查询的对象。

  • path (Array|string) − 要检查的路径。

输出

  • (boolean) −如果路径存在则返回 true,否则返回 false。

示例

var _ = require('lodash');
var object = { 'a': [{ 'b': { 'c': 3 } }] };
var result = _.has(object, 'a');

console.log(result);

result = _.has(object, ['b']);
console.log(result);

将上述程序保存在 tester.js 中。运行以下命令执行此程序。

命令

\>node tester.js

输出

true
false

lodash_object.html