Skip to content

Instantly share code, notes, and snippets.

@ralucas
Created December 27, 2017 21:19
Show Gist options
  • Save ralucas/452d376a33c480e77416d69355db8296 to your computer and use it in GitHub Desktop.
Save ralucas/452d376a33c480e77416d69355db8296 to your computer and use it in GitHub Desktop.
/**
* Searches through an array finding all
* occurences of predicates
* @param arr {array}
* @param searchVals {array}
* @return findCountData {object}
*/
function findAll (arr: any[], searchVals: any[]) {
let findCounts = searchVals.reduce((acc, val) => {
acc[val] = 0;
return acc;
}, {});
let findCount = 0;
let i = 0, j = 0;
const lenArr = arr.length;
const lenVals = searchVals.length;
for (i; i < lenArr; i += 1) {
for (j; j < lenVals; j += 1) {
if (arr[i] == searchVals[j]) {
findCounts[searchVals[j]] += 1;
findCount += 1;
}
}
}
return {
findCount,
findCounts
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment