count number of each element in array javascript
var arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4];
var counts = {};
for (var i = 0; i < arr.length; i++) {
var num = arr[i];
counts[num] ? counts[num] + 1 : 1;
}
console.log(counts[5], counts[2], counts[9], counts[4]);
Also in JavaScript:
- javascript throw new error
- clear session storage on refresh
- how to make item not dragable in react-sortablejs
- express router middleware
- form taglib in jsp
- flutter CircularProgressIndicator
- how to check if a number is negative in p5.js
- how to write and read a text file in jdiscord.js
- make indexOF in js
- loop key in object
- bootstrap set active tab javascript
- get ascii value of char javascript
- evaluatejsonpath array json
- toastify js
- return early pattern for functions javascript
- exceljs read file example
- how to make use of nodejs single thread to read files and attach data
- how can auto download window print in javascript
- react native post api method
- javascript render jsx element x many times
- alpine js
- Mobx-react console warning related observer
- loop in javascript
- check if element is last child jquery