Lodash - hasIn 方法

语法

_.hasIn(object, path)

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

参数

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

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

输出

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

示例

var _ = require('lodash');
var object = _.create({ 'a': _.create({ 'b': 2 }) });
var result = _.hasIn(object, 'a');

console.log(result);

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

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

命令

\>node tester.js

输出

true
true

lodash_object.html