Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

count the number of times a same value appears in a javascript array

var arr = [2, 3, 1, 3, 4, 5, 3, 1];

function getOccurrence(array, value) {
    var count = 0;
    array.forEach((v) => (v === value && count++));
    return count;
}

console.log(getOccurrence(arr, 1));  // 2
console.log(getOccurrence(arr, 3));  // 3
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #count #number #times #appears #javascript #array
ADD COMMENT
Topic
Name
7+9 =