Mario
Mario

Reputation: 2739

unique values in array count

I have an array like the following

var data = [
            ["1"," 101_30.1.101_34","0.0200112629","mm/s","[OK]"],
            ["1"," 101_30.1.101_35","0.0146548533","mm/s","[OK]"],
            ["1"," 101_30.1.101_45","0.0146548533","mm/s","[OK]"],
            ["1"," 101_42.2.101_43","0.0101406257","mm/s","[OK]"],
            ["2"," 102_17.3.102_38","5.1719756","mm/s","[WA]"],
            ["2"," 102_17.3.102_39","3.5886707","mm/s","[WA]"],
            ["2"," 102_17.3.102_44","9.4615074E-4","mm/s","[OK]"],
            ["2"," 102_40.4.102_41","4.8159785","mm/s","[OK]"],
            ["3"," 204_15","3.8374166","mA","[OK]"],
            ["4"," 501_11","1027.5156","RPM","[WA]"]
           ]

What im trying to do is find how many unique array there are. Example 1=4,2=4,3=1,4=1

The data is coming from a database, so the number of arrays can always change.

Here is a simple jsfiddle of what im talking about JsFiddle

Upvotes: 1

Views: 6790

Answers (1)

gen_Eric
gen_Eric

Reputation: 227200

Try something like this:

var count = {};
$.each(data, function(){
    var num = this[0]; // Get number
    count[num] = count[num]+1 || 1; // Increment counter for each value
});
console.log(count); // {1: 4, 2: 4, 3: 1, 4: 1}

Upvotes: 7

Related Questions