Menu

gitpiper

indexOfAll javascript Code Snippet in 2024

arrayintermediate

Last Updated: 21 April 2024

Finds all indexes of val in an array. If val never occurs, returns an empty array.

  • Use Array.prototype.reduce() to loop over elements and store indexes for matching elements.
const indexOfAll = (arr, val) => arr.reduce((acc, el, i) => (el === val ? [...acc, i] : acc), []);
indexOfAll([1, 2, 3, 1, 2, 3], 1); // [0, 3] indexOfAll([1, 2, 3], 4); // []

javascript snippet similar to indexOfAll For You in April 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️