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.