diff --git a/README.md b/README.md index 0c1b60618..46afd17cd 100644 --- a/README.md +++ b/README.md @@ -342,6 +342,15 @@ average(1, 2, 3); +### _Uncategorized_ + +
+View contents + +* [`indexOfAll`](#indexofall) + +
+ --- ## 🔌 Adapter @@ -5130,6 +5139,31 @@ yesNo('Foo', true); // true
[⬆ Back to top](#table-of-contents) +--- + ## _Uncategorized_ + +### indexOfAll + +Returns all indices of `val` in an array. If `val` never occurs, returns `[-1]`. + +Use `Array.forEach()` to loop over elements and `Array.push()` to store indices for matching elements. +Return `[-1]` if `length` of the array of indices is `0`, otherwise return the array of indices. + +```js +const indexOfAll = (arr, val) => { + const indices = []; + arr.forEach((el, i) => el === val && indices.push(i)); + return indices.length ? indices : [-1]; +}; +``` + +```js +indexOfAll([1, 2, 3, 1, 2, 3], 1); // [0,3] +indexOfAll([1, 2, 3], 4); // [-1] +``` + +
[⬆ back to top](#table-of-contents) + ## Collaborators diff --git a/docs/index.html b/docs/index.html index 4e7fe2081..9df073a6b 100644 --- a/docs/index.html +++ b/docs/index.html @@ -40,7 +40,7 @@ },1700); } }, 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 ]
@@ -1104,4 +1104,11 @@ console.log<
 yesNo('yes'); // true
 yesNo('No'); // false
 yesNo('Foo', true); // true
-
\ No newline at end of file +

Uncategorized

indexOfAll

Returns all indices of val in an array. If val never occurs, returns [-1].

Use Array.forEach() to loop over elements and Array.push() to store indices for matching elements. Return [-1] if length of the array of indices is 0, otherwise return the array of indices.

const indexOfAll = (arr, val) => {
+  const indices = [];
+  arr.forEach((el, i) => el === val && indices.push(i));
+  return indices.length ? indices : [-1];
+};
+
indexOfAll([1, 2, 3, 1, 2, 3], 1); // [0,3]
+indexOfAll([1, 2, 3], 4); // [-1]
+
\ No newline at end of file diff --git a/snippets/indexOfAll.md b/snippets/indexOfAll.md index d453e8a5f..e06f58946 100644 --- a/snippets/indexOfAll.md +++ b/snippets/indexOfAll.md @@ -5,16 +5,15 @@ Returns all indices of `val` in an array. If `val` never occurs, returns `[-1]`. Use `Array.forEach()` to loop over elements and `Array.push()` to store indices for matching elements. Return `[-1]` if `length` of the array of indices is `0`, otherwise return the array of indices. -``` js +```js const indexOfAll = (arr, val) => { const indices = []; - arr.forEach((el, i) => el === val && indices.push(i)) + arr.forEach((el, i) => el === val && indices.push(i)); return indices.length ? indices : [-1]; }; - ``` -``` js -indexOfAll([1,2,3,1,2,3],1); // [0,3] -indexOfAll([1,2,3],4); // [-1] +```js +indexOfAll([1, 2, 3, 1, 2, 3], 1); // [0,3] +indexOfAll([1, 2, 3], 4); // [-1] ``` diff --git a/tag_database b/tag_database index aee8519c5..1edc5b964 100644 --- a/tag_database +++ b/tag_database @@ -62,6 +62,7 @@ head:array hexToRGB:utility,string,math,advanced hide:browser,css httpsRedirect:browser,url +indexOfAll:uncategorized initial:array initialize2DArray:array initializeArrayWithRange:array,math