Lodash _.keys() 方法
_.keys()方法用于返回给定对象的所有键的列表。
句法:
_.keys(object)
参数:此方法接受如上所述和如下所述的单个参数:
- object:此参数保存对象元素。
返回值:此方法返回给定对象的所有键的数组。
示例 1:
Javascript
// Requiring the lodash library
const _ = require("lodash");
// Given object
var obj = { 'a': 1, 'b': 2, 'c': 2 };
// Use of _.keys method
console.log(_.keys(obj));
Javascript
// Requiring the lodash library
const _ = require("lodash");
// Given object
var obj = {
Name: "GeeksforGeeks",
password: "gfg@1234",
username: "your_geeks"
}
// Use of _.keys method
console.log(_.keys(obj));
输出:
['a', 'b', 'c']
示例 2:
Javascript
// Requiring the lodash library
const _ = require("lodash");
// Given object
var obj = {
Name: "GeeksforGeeks",
password: "gfg@1234",
username: "your_geeks"
}
// Use of _.keys method
console.log(_.keys(obj));
输出:
["Name", "password", "username"]