HTML example code:
<!DOCTYPE HTML> <html> <body> <script> var arr = [1, 2, 3, 5, 2, 8, 9, 2]; var count = arr.length; console.log(count); </script> </body> </html>
Q: How to count certain elements in Array?
Answer: Let’s find how many 2s are in the Array:-
[1, 2, 3, 5, 2, 8, 9, 2]
It’s very simple with a for loop, match given item with all elements of Array and count it:
Do comment if you have any doubts and suggestions on this JS Array topic.
Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version