DEV Community

Cover image for JavaScript: How To Find The Number Of Times An Item Appears In An Array
Lucretius Biah
Lucretius Biah

Posted on • Originally published at kudadam.com

JavaScript: How To Find The Number Of Times An Item Appears In An Array

If you are coming from a Python background, then you obviously know about the count method in the list class.

In JavaScript, there is nothing as the count method. For those of you who don't know what the count method does is that, it counts the number of times an item appears in an array. For example, say we are having an array called numbers and we want to find the number of times the number two appeared in the array, this is how we would do it in Python.

numbers = [1,2,3,4,5,2,3,4,2,4,2]
print(numbers.count(2)) # => 4
Enter fullscreen mode Exit fullscreen mode

Since the number two appeared four times in the array, the function would return the number 4.

Since this method does not exist in JavaScript, I decided to create my own count method.

Code

Array.prototype.count = function(item){ 
            let appearance = 0; //This is the default value
            this.forEach(index=>{
                if (index === item)
                    appearance++
            });
            return appearance;
        }
Enter fullscreen mode Exit fullscreen mode

Explanation

In the code above, I extended the Array class and added a new method called count through the prototype. The method takes an argument called item. Then it iterates through all the values in the array and check if they correspond the the item, if they do, it increases the appearance counter by 1. Finally, it returns the appearance's value.

Using The Code

In order to use the code, just create an array and call the count method like this.

let names = ["David","Walsh","David","Tania","Lucretius"];
names.count("David") //=> 2
Enter fullscreen mode Exit fullscreen mode

Happy Coding Fellas πŸ˜„

Discussion (6)

Collapse
jonrandy profile image
Jon Randy

Extending prototypes in this way is dangerous and should be done with great care. Don't forget, you are extending a core part of JS that is relied on by pretty much everything else. There's no guarantee that your code will not conflict with other code that attempts to do something similar, or will be compatible with future updates of JS itself.

That said, there are ways to do this kind of thing safely. I recently wrote a library that can be used to help you safely extend native prototypes in this manner. I call it Metho. Take a look if you're interested πŸ™‚

Collapse
kudadam profile image
Lucretius Biah Author

Hey @jonrandy metho seems really cool. I write an article about it soon

Collapse
frankwisniewski profile image
Frank Wisniewski
let names = ["David","Walsh","David","Tania","Lucretius"];
names.filter(x=>x==="David").length // => 2
Enter fullscreen mode Exit fullscreen mode
Collapse
kudadam profile image
Lucretius Biah Author

Hey @frankwisniewski ,
You method is much simpler. I guess I will have to update my code.
Anyways, thanks for the helpπŸ₯³πŸ₯³

Collapse
xd1gital profile image
X Digital

another way to do it:

Array.prototype.count = function(item) {
  return this.reduce((a, x) => (x == item) ? a + 1 : a, 0)
}
Enter fullscreen mode Exit fullscreen mode
Collapse
kudadam profile image
Lucretius Biah Author

WowπŸ€”