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


Lodash _.invokeMap()用法及代碼示例

Lodash是一個JavaScript庫,可在underscore.js頂部使用。 Lodash幫助處理數組,集合,字符串,對象,數字等。

_.invokeMap()方法在集合中每個元素的給定路徑上調用該方法,並返回每個調用方法的結果的數組。可以使用args參數為每個調用的方法提供其他參數。給定的路徑也可以是綁定到集合中每個元素的函數。

用法:

_.invokeMap( collection, path, args )

參數:此方法接受上述和以下所述的三個參數:

  • collection:此參數保存必須迭代的集合。
  • path:此參數保存每次迭代要調用的方法或函數的路徑。
  • args:此參數包含用於調用每個方法的參數。

返回值:此方法返回結果數組。



範例1:

// Requiring the lodash library  
const _ = require("lodash");  
       
// Original array  
let obj = [[6, 2, 8], [2, 1, 0]]; 
   
// Using the _.invokeMap() method 
let gfg1 = _.invokeMap(obj, 'sort'); 
  
// Printing the output  
console.log(gfg1);

輸出:

[ [ 2, 6, 8], [ 0, 1, 2 ] ]

範例2:

// Requiring the lodash library  
const _ = require("lodash");  
       
// Original array  
let obj = [628, 210]; 
   
// Using the _.invokeMap() method  
let gfg1 = _.invokeMap(obj, String.prototype.split, ''); 
  
// Printing the output  
console.log(gfg1);

輸出:

[ [ '6', '2', '8'], [ '2', '1', '0' ] ]

範例3:

// Requiring the lodash library  
const _ = require("lodash");  
       
// Original array  
let obj = ['srqp', 'tuvw']; 
let obj1 = [ ['c', 'b', 'a'], ['f', 'e', 'd'] ]; 
   
// Using the _.invokeMap() method 
let gfg = _.invokeMap(obj, String.prototype.split, ''); 
let gfg1 = _.invokeMap(obj1, 'sort'); 
  
// Printing the output  
console.log(gfg, gfg1);

輸出:

[ [ 's', 'r', 'q', 'p'], [ 't', 'u', 'v', 'w' ] ]
[ [ 'a', 'b', 'c'], [ 'd', 'e', 'f' ] ]




相關用法


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