_.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));
輸出:
{ '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"}
相關用法
- d3.js d3.continuous.invert()用法及代碼示例
- CSS invert()用法及代碼示例
- Underscore.js _.invert()用法及代碼示例
- d3.js pow.invert()用法及代碼示例
- d3.js log.invert()用法及代碼示例
- d3.js time.invert()用法及代碼示例
- d3.js transform.invert()用法及代碼示例
- Lodash _.method()用法及代碼示例
- Lodash _.sneq()用法及代碼示例
- Lodash _.toQuery()用法及代碼示例
- Lodash _.uniqWith()用法及代碼示例
- Lodash _.xorWith()用法及代碼示例
- Lodash _.head()用法及代碼示例
- Lodash _.remove()用法及代碼示例
- Lodash _.pullAt()用法及代碼示例
- Lodash _.pullAll()用法及代碼示例
- Lodash _.pull()用法及代碼示例
- Lodash _.nth()用法及代碼示例
- Lodash _.takeRight()用法及代碼示例
- Lodash _.take()用法及代碼示例
- Lodash _.sortedLastIndex()用法及代碼示例
- Lodash _.fromPairs()用法及代碼示例
注:本文由純淨天空篩選整理自sanjoy_62大神的英文原創作品 Lodash _.invert() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。