Q:

How to count comma separated values in javascript

Here i've a number of values that are comma separated. now i just want to count them.

the values are
"160,159,158,157,156,155,143,141,140,139"

like:
160: 1
159: 2
158: 3 and so on..

And here in my try
 var checkValues = $('input[name=checkboxlist]:checked').map(function()
                   {
                        return $(this).val();
                   }).get();
 var i = '"'+checkValues+'"'.split(',');
 alert(i);

Please guide me, where am i going wrong?

A:

I think you want the count not the sum.
split it on , and get the length

var val="160,159,158,157,156,155,143,141,140,139";
console.log("Count : ",val.split(",").length);

If you want the index of each value and count
then use

var val="160,159,158,157,156,155,143,141,140,139";
val.split(",").forEach( (x,y) => console.log(x," : ",y+1));

the y+1 is there because y is position and starts from 0

