當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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)); 

輸出:

{ '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"}

相關用法


注:本文由純淨天空篩選整理自sanjoy_62大神的英文原創作品 Lodash _.invert() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。