-2

enter image description hereenter image description heretestI have an array like this

var arr1 = [3, 'a', 'a', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3];

I want the output to be like a - 5 times I tried like below:-

var count = 0;
for (var i = 0; i < arr.length; i++) {
    arr.indexOf(arr[i] > -1) {
        count++;
    }
}

But not worked,can anyone please help me.Thanks.

MMR
  • 2,869
  • 13
  • 56
  • 110
  • specifically in your code, you need to give the `from-index` value while calling `indexOf`, for example `( arr.indexOf( arr[i], i ) > -1)` – gurvinder372 Sep 27 '17 at 10:59

2 Answers2

1
arr1.filter(x => x > -1).length

or you can use reduce instead (more efficient, a bit less readable).

Kraylog
  • 7,383
  • 1
  • 24
  • 35
0

Using Loadash

var resul = _.countBy([3, 'a', 'a', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3]);
console.log(resul);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396