Lodash - hasIn 方法



語法

_.hasIn(object, path)

檢查路徑是否為物件的直接屬性或繼承屬性。

引數

  • object (物件) - 要查詢的物件。

  • path (陣列 | 字串) - 要檢查的路徑。

輸出

  • (布林值) - 如果路徑存在,則返回 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.htm
廣告
© . All rights reserved.