Created
December 27, 2017 21:19
-
-
Save ralucas/452d376a33c480e77416d69355db8296 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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