Open In App

Lodash _.sortedUniqBy() Method

Improve
Improve
Like Article
Like
Save
Share
Report

Lodash _.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:

  • 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 return the new duplicate free array.

Example 1: In this example, we are getting an array that has unique elements by the use of the lodash _.sortedUniqBy() method.

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: In this example, we are getting an array that has unique elements by the use of the lodash _.sortedUniqBy() method.

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.


Last Updated : 30 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads