Lodash _.property() 方法
Lodash _.property()方法用于返回一个函数,该函数将返回任何传入对象的指定属性。
句法:
_.property(path)
参数:此方法接受一个如上所述和如下所述的参数:
- path:此参数保存要获取的属性的路径。
返回值:此方法返回一个新的访问函数。
示例 1:
Javascript
// Requiring the lodash library
const _ = require("lodash");
var info = {
Company: 'GeeksforGeeks',
Address: 'Noida'
};
// Use of _.property() method
let gfg = _.property('Company')
(info) === 'GeeksforGeeks'
// Printing the output
console.log(gfg);
Javascript
// Requiring the lodash library
const _ = require("lodash");
var info = {
Company: { name: 'GeeksforGeeks' },
Contact: { Address:
{
AddressInfo: 'Noida'
}
}
};
// Use of _.property() method
var propInfo = _.property(['Contact',
'Address', 'AddressInfo', ]);
// Printing the output
console.log(propInfo(info));
输出:
true
示例 2:
Javascript
// Requiring the lodash library
const _ = require("lodash");
var info = {
Company: { name: 'GeeksforGeeks' },
Contact: { Address:
{
AddressInfo: 'Noida'
}
}
};
// Use of _.property() method
var propInfo = _.property(['Contact',
'Address', 'AddressInfo', ]);
// Printing the output
console.log(propInfo(info));
输出:
Noida