1

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

4
  • Are you looking to just find duplicates of the first value of the array? Commented Feb 29, 2012 at 16:07
  • ...and your question is? Commented Feb 29, 2012 at 16:07
  • 1
    Just iterate over the array and count the occurrences of the first element of each element. Commented Feb 29, 2012 at 16:08
  • Yes, basically im looking for duplicates of the first value. I dont want to remove them, i just need to count them. Commented Feb 29, 2012 at 16:11

1 Answer 1

7

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}
Sign up to request clarification or add additional context in comments.

2 Comments

@RocketHazmat I tried this but for some reason it's only counting until 9? ex: {1: 84, 2: 97, 3: 31, 4: 57, 5: 28, 6: 21, 7: 28, 8: 25, 9: 18} Also, what could I add so that duplicates are deleted?
@Patrick: This question is almost 3 years old. Why not ask a new one? If you don't want to, at least tell me what you are trying to do. What do you mean by "only counting until 9"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.