countOccurrences
The countOccurrences ()
function counts the number of occurrences of a value in an array..
Parameters
Parameter | Type | Description |
---|---|---|
arr | Array | The array to search for occurrences of val . |
val | String | The value to search for in the array. |
Returns
Number
: The number of occurrences ofval
inarr
.
Throws
Error
: Ifarr
is not an array.
Example
const utils = require('utils-core.js');
const arr = [1, 2, 3, 4, 1, 2, 1];
const val = 1;
const count = utils.arrays.countOccurrences(arr, val);
console.log(count); // Output: 3