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


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

_.sortedUniqBy方法用於返回可以插入元素的數組的最低索引,並保持其排序順序。此外,此方法類似於_.uniqBy,但它是為排序數組設計和優化的。

用法:

_.sortedUniqBy(array, [iteratee])

參數:該方法接受上述和以下所述的兩個參數:

  • array:此參數保存要檢查的數組。
  • [iteratee = _。identity](函數):此參數保存每個元素調用的iteratee。

返回值:此方法用於返回新的重複自由數組。

範例1:在這裏,const _ = require(‘lodash’)用於將lodash庫導入文件中。



javascript

// Requiring the lodash library  
const _ = require("lodash");  
      
// Original array  
let y = ([1.1, 1.2, 2.1, 2.3, 2.4, 3.5]) 
      
// Use of _.sortedUniqBy()  
// method  
let index =  _.sortedUniqBy(y, Math.floor);  
      
// Printing the output  
console.log(index);

輸出:

[1.1, 2.1, 3.5]

範例2:

javascript

// Requiring the lodash library  
const _ = require("lodash");  
      
// Original array  
let y = ([112.1, 112.2, 122.1, 122.3, 122.4, 132.5]) 
      
// Use of _.sortedUniqBy()  
// method  
let index =  _.sortedUniqBy(y, Math.floor);  
      
// Printing the output  
console.log(index);

輸出:

[112.1, 112.1, 132.5]

注意:在正常的JavaScript中這將無法正常工作,因為它需要安裝庫lodash。

相關用法


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