Monday, July 15, 2024
HomeLanguagesJavascriptCount occurrences of all items in an array in JavaScript

Count occurrences of all items in an array in JavaScript

In this article, we will see how to count occurrences of all items in an array in Javascript. One of the most common methods of doing this is by using the traditional for loop. In Javascript, we have another modern approach to iterate the array which is by using the forEach method.

Counting the occurrences of all the items in an array can be done in the following ways:

Approach 1: Using the Javascript forEach() method:

The arr.forEach() method calls the provided function once for each element of the array. The provided function may perform any kind of operation on the elements of the given array. 

Syntax:

array.forEach(callback(element, index, arr), thisValue)

Example: In this example, we will iterate over the elements of an array using the forEach() method and count the number of occurrences of all the elements of the array and print them in the console.

Javascript




let arr = [
    'neveropen', 2, 'neveropen', 2, 'Javascript', 4,
    'Javascript', 5, 'for', 6, 'Javascript', 1,
    'for', 5, 7, 8, 'for'
];
 
const counter = {};
 
arr.forEach(ele => {
    if (counter[ele]) {
        counter[ele] += 1;
    } else {
        counter[ele] = 1;
    }
});
 
console.log(counter)


Output

{
    '1': 1,
    '2': 2,
    '4': 1,
    '5': 2,
    '6': 1,
    '7': 1,
    '8': 1,
    neveropen: 2,
    Javascript: 3,
    for: 3
}

Approach 2: Using reduce() method:

The Javascript arr.reduce() method is used to reduce the array into a single value and executes the provided function for each value of the array (from left-to-right) and the return value of the function is stored in an accumulator. 

Syntax:

array.reduce( 
    function(total, currentValue, currentIndex, arr), 
    initialValue 
)

Example: In this example, we will iterate over the elements of an array using the reduce() method and count the number of occurrences of all the elements of the array and print them in the console.

Javascript




const arr = [
    'neveropen', 2, 'neveropen', 2, 'Javascript', 4,
    'Javascript', 5, 'for', 6, 'Javascript', 1,
    'for', 5, 7, 8, 'for'
];
 
let count = arr.reduce(function (value, value2) {
    return (
        value[value2] ? ++value[value2] :(value[value2] = 1),
        value
    );
}, {});
 
console.log(count);


Output

{
    '1': 1,
    '2': 2,
    '4': 1,
    '5': 2,
    '6': 1,
    '7': 1,
    '8': 1,
    neveropen: 2,
    Javascript: 3,
    for: 3
}

Approach 3: Using filter() method:

The JavaScript Array filter() Method is used to create a new array from a given array consisting of only those elements from the given array which satisfy a condition set by the argument method. 

Syntax:

array.filter(callback(element, index, arr), thisValue)

Example: In this example, we will use the array filter() method to check the occurrences of the elements of the array and print them in the console.

Javascript




const arr = [
    'neveropen', 2, 'neveropen', 2, 'Javascript', 4,
    'Javascript', 5, 'for', 6, 'Javascript', 1,
    'for', 5, 7, 8, 'for'
];
 
const itemCounter = (value, index) => {
    return value.filter((x) => x == index).length;
};
 
console.log(itemCounter(arr, 'neveropen'))


Output

2

Approach 4: Using for…of loop:

The for…of loop iterates over the iterable objects (like Array, Map, Set, arguments object, …, etc), invoking a custom iteration hook with statements to be executed for the value of each distinct property.

Syntax:

for ( variable of iterableObjectName) {
    ...
}

Example: In this example, we will iterate over the elements of an array using the for…of loop and count the number of occurrences of all the elements of the array and print them in the console.

Javascript




const arr = [
    'neveropen', 2, 'neveropen', 2, 'Javascript', 4,
    'Javascript', 5, 'for', 6, 'Javascript', 1,
    'for', 5, 7, 8, 'for'
];
 
const count = {};
 
for (let ele of arr) {
    if (count[ele]) {
        count[ele] += 1;
    } else {
        count[ele] = 1;
    }
}
 
console.log(count);


Output

{
    '1': 1,
    '2': 2,
    '4': 1,
    '5': 2,
    '6': 1,
    '7': 1,
    '8': 1,
    neveropen: 2,
    Javascript: 3,
    for: 3
}

Dominic Rubhabha Wardslaus
Dominic Rubhabha Wardslaushttps://neveropen.dev
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments