diff --git a/README.md b/README.md index d67863f68..c0e57d210 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,7 @@ average(1, 2, 3); * [`shuffle`](#shuffle) * [`similarity`](#similarity) * [`sortedIndex`](#sortedindex) +* [`sortedLastIndex`](#sortedlastindex) * [`symmetricDifference`](#symmetricdifference) * [`symmetricDifferenceBy`](#symmetricdifferenceby) * [`symmetricDifferenceWith`](#symmetricdifferencewith) @@ -1801,6 +1802,37 @@ sortedIndex([30, 50], 40); // 1
[⬆ Back to top](#table-of-contents) +### sortedLastIndex + +Returns the highest index at which value should be inserted into array in order to maintain its sort order. + +Check if the array is sorted in descending order (loosely). +Use `Array.map()` to map each element to an array with its index and value. +Use `Array.filter()` to find all possible positions where the element could be inserted, `Array.slice(-1)` to get the last one. + +```js +const sortedLastIndex = (arr, n) => { + const isDescending = arr[0] > arr[arr.length - 1]; + const index = arr + .map((val, i) => [i, val]) + .filter(el => (isDescending ? n >= el[1] : n >= el[1])) + .slice(-1)[0][0]; + return index === -1 ? arr.length : index; +}; +``` + +
+Examples + +```js +sortedLastIndex([10, 20, 30, 30, 40], 30); // 3 +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### symmetricDifference Returns the symmetric difference between two arrays. diff --git a/docs/index.html b/docs/index.html index 213aae959..f4b41a2a5 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -342,6 +342,15 @@ Object.assig
 };
 
sortedIndex([5, 3, 2, 1], 4); // 1
 sortedIndex([30, 50], 40); // 1
+

sortedLastIndex

Returns the highest index at which value should be inserted into array in order to maintain its sort order.

Check if the array is sorted in descending order (loosely). Use Array.map() to map each element to an array with its index and value. Use Array.filter() to find all possible positions where the element could be inserted, Array.slice(-1) to get the last one.

const sortedLastIndex = (arr, n) => {
+  const isDescending = arr[0] > arr[arr.length - 1];
+  const index = arr
+    .map((val, i) => [i, val])
+    .filter(el => (isDescending ? n >= el[1] : n >= el[1]))
+    .slice(-1)[0][0];
+  return index === -1 ? arr.length : index;
+};
+
sortedLastIndex([10, 20, 30, 30, 40], 30); // 3
 

symmetricDifference

Returns the symmetric difference between two arrays.

Create a Set from each array, then use Array.filter() on each of them to only keep values not contained in the other.

const symmetricDifference = (a, b) => {
   const sA = new Set(a),
     sB = new Set(b);