1

I have logfile data, and I want to use jQuery to count how many entries each "site" has. Further, I'd like to do something (maybe change the background color of) sites that have multiple entries:

<tr><td class='site'>123456</td><td>70</td><td>10.32</td><td>2014-03-04</td></tr>
<tr><td class='site'>123457</td><td>73</td><td>8.95</td><td>2014-03-03</td></tr>
<tr><td class='site'>123458</td><td>61</td><td>1.37</td><td>2014-03-05</td></tr>
<tr><td class='site'>123457</td><td>68</td><td>12.26</td><td>2014-03-04</td></tr>
<tr><td class='site'>123457</td><td>95</td><td>15.31</td><td>2014-03-05</td></tr>
<tr><td class='site'>123456</td><td>58</td><td>1.31</td><td>2014-03-05</td></tr>
<tr><td class='site'>123459</td><td>65</td><td>11.06</td><td>2014-03-05</td></tr>

So, from the html above, I would put the following totals into a div:

Site    Issues
123456    2
123457    3
123458    1
123459    1

...and I would highlight the two sites that have multiple entries (123456 and 123457) in the table. Can you point me in the right direction?

P.S. I'm already using "tablesorter" to sort the rows. Really handy plugin!

3 Answers 3

2
var totals = {};
$("td.site").each(function() {
    var site = $(this).text();
    if (totals[site]) {
        totals[site]++;
    } else {
        totals[site] = 1;
    }
}

Now the object totals contains the counts you want.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much - I was close, but I wanted to match text for some reason.
1

Here is one of the ways you can do it. Jsbin Demo

var result = {};

$('.site').each(function(index, element) {
  var $el = $(element);
  var site = $el.html();
  result[site] = result[site] || 0;
  result[site]++; 

  //To highlight sites with multiple issues
  if (result[site] > 1) {
    $el.css({background: 'green'}); //Or something else
  } 
});

console.log(result);

Update

For a corner case caught by @Eduardo and he suggested the fix as well in the comment. We should use this to highlight element otherwise we will miss to select the first element in multiple entries.

$('.site:contains('+site+')').css({'color': 'green'}); //Or something else

10 Comments

Where do you set numIssues?
result[site][numIssues] = result[site][numIssues] || 0;
That doesn't set the variable numIssues. You also never initalize the variable els.
I think you mean result[site].numIssues and result.[site].els.
@Barmar yes..I made the typo..You are right..Will update the code.
|
1
var sites = [];
var rows = $("tr");
for(var i in rows){
    var site = $("td.site", rows[i]).text();
    // if I've already saved this site, i go to the next one
    if(sites[site])continue;

    // i get how many times the site is repeated
    var count = $("td:contains('"+site+"')").length;

    // save result in an array
    sites[site] = count;
    // or display the result somehow
}

Comments

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.