Lodash _.invert() 方法
_.invert()方法用于返回对象的副本,其中对象键转换为值,对象值转换为键。如果对象包含重复值,则后续值将覆盖属性分配。
句法:
_.invert(object)
参数:此方法接受如上所述和如下所述的单个参数:
- object:此参数保存要反转的对象。
返回值:此方法返回新的反转对象。
示例 1:
Javascript
// Requiring the lodash library
const _ = require("lodash");
// Given object
var obj = { 'a': 1, 'b': 2, 'c': 3 };
// Use of _.invert method
console.log(_.invert(obj));
Javascript
// Requiring the lodash library
const _ = require("lodash");
// Given object
var obj = { 'a': 1, 'b': 2, 'c': 2 };
// Use of _.invert method
console.log(_.invert(obj));
Javascript
/// Requiring the lodash library
const _ = require("lodash");
// Given object
var obj = {
Name: "GeeksforGeeks",
password: "gfg@1234",
username: "your_geeks"
}
// Use of _.invert method
console.log(_.invert(obj));
输出:
{ '1': 'a', '2': 'b', '3': 'c'}
示例 2:
Javascript
// Requiring the lodash library
const _ = require("lodash");
// Given object
var obj = { 'a': 1, 'b': 2, 'c': 2 };
// Use of _.invert method
console.log(_.invert(obj));
输出:
{ '1': 'a', '2': 'c' }
示例 3:
Javascript
/// Requiring the lodash library
const _ = require("lodash");
// Given object
var obj = {
Name: "GeeksforGeeks",
password: "gfg@1234",
username: "your_geeks"
}
// Use of _.invert method
console.log(_.invert(obj));
输出:
{GeeksforGeeks: "Name",
gfg@1234: "password",
your_geeks: "username"}