在这里,我有一些逗号分隔的值。现在我只想数一数。

值是
"160,159,158,157,156,155,143,141,140,139"
Run Code Online (Sandbox Code Playgroud)
喜欢:
160: 1
159: 2
158: 3 and so on..
Run Code Online (Sandbox Code Playgroud)
而在我的尝试
var checkValues = $('input[name=checkboxlist]:checked').map(function()
{
return $(this).val();
}).get();
var i = '"'+checkValues+'"'.split(',');
alert(i);
Run Code Online (Sandbox Code Playgroud)
请指导我,我要去哪里错了?
我认为您想要的不是总数。
将其拆分,并获得长度
var val="160,159,158,157,156,155,143,141,140,139";
console.log("Count : ",val.split(",").length);Run Code Online (Sandbox Code Playgroud)
如果要每个值的索引和计数
然后使用
var val="160,159,158,157,156,155,143,141,140,139";
val.split(",").forEach( (x,y) => console.log(x," : ",y+1));Run Code Online (Sandbox Code Playgroud)
的y+1是那里,因为y是位置,从0开始