The countInArray function may be an option for you
function countInArray(array, what) {
return array.filter(item => item == what).length;
}
Or something like this, this may be better for you to understand the code and adjust somethings where you want to! :
var list = [2, 1, 4, 2, 1, 1, 4, 5];
function countInArray(array, what) {
var count = 0;
for (var i = 0; i < array.length; i++) {
if (array[i] === what) {
count++;
}
}
return count;
}
countInArray(list, 2); // returns 2
countInArray(list, 1); // returns 3