Friday, October 17, 2025
HomeLanguagesJavascriptLodash _.sortedUniqBy() Method

Lodash _.sortedUniqBy() Method

The _.sortedUniqBy method is used to return the lowest index of the array where an element can be inserted and maintain its sorted order. Also, this method is like _.uniqBy except that it’s designed and optimized for sorted arrays.

Syntax:

_.sortedUniqBy(array, [iteratee])

Parameters: This method accepts two parameters as mentioned above and described below:

  • array: This parameter holds the array to inspect.
  • [iteratee=_.identity] (Function): This parameter holds the iteratee invoked per element.

Return Value: This method is used to returns the new duplicate free array.

Example 1: Here, const _ = require(‘lodash’) is used to import the lodash library into the file.

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


Output:

[1.1, 2.1, 3.5]

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


Output:

[112.1, 112.1, 132.5]

Note: This will not work in normal JavaScript because it requires the library lodash to be installed.

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS