The sortedLastIndexBy
function from the Lodash library allows you to find the index position to insert an element in a sorted array based on a computed value. Here is an example of how to use it in Typescript:
index.ts421 chars21 lines
In this example, we import the sortedLastIndexBy
function and define an array of Person
objects. Then we call the sortedLastIndexBy
function, passing in the array, the element to insert, and a function which computes the value to sort by (in this case, the age
property). The sortedLastIndexBy
function returns the index position at which to insert the element in the sorted array.
Note that because sortedLastIndexBy
returns an index position rather than actually inserting the element into the array, you will need to use splice or other array functions to actually insert the element.
gistlibby LogSnag