Group and count HTML elements by data attribute in jQuery

前端 未结 5 475
小鲜肉
小鲜肉 2020-12-19 05:58

I\'m trying to group each instance of a data attribute into a list from the number of occurrences of each attribute in a page, using jQuery. Similar to grouping categories o

相关标签:
5条回答
  • 2020-12-19 06:18
    $(function() {
        var categories = {};
        $(".categories li").each(function() {
            var category = $(this).data("category");
    
            if (categories.hasOwnProperty(category) === false) {
                categories[category] = 1;
            } else {
                categories[category]++;
            }
        });
    
        console.log(categories);
    })
    

    Example

    0 讨论(0)
  • 2020-12-19 06:20

    If data attribute is on span or div inside <tr> then you have to use find. Find is more costly in term of DOM search. Is better to just add class on that elements where you have data-attribute and loop through and get information.

    /*
     * fetch labels from list by data attribute
     * 
     */
    
    function fetch_labels() {
    
        var leadssource = {},
                lead;
    
        $('#leads-list tr').each(function(i, el) {
    
            lead = $(el).find('[data-leadsource]').data('leadsource');
            if (leadssource.hasOwnProperty(lead)) {
                leadssource[lead] += 1;
            }
            else {
                leadssource[lead] = 1;
            }
        });
    
        return leadssource;
    }
    
    0 讨论(0)
  • 2020-12-19 06:27

    One approach is to use jQuery's each method to load each of the sections into array you define prior. Setup your conditions within the each method, collect the data and do whatever your heart desires with each list.

    With this method you can set as many variables within this one each method to check for. For example, if there was another attribute you wanted to check within each li element, you can do that and make more lists.

    The console.log below, simply gives you a visual on what is stored within each array you define. Good Luck!

    And a quick jsFiddle demo.

    var cat1 = [];
    var cat2 = [];
    var cat3 = [];
    
    $('li').each(function () {
    
    var attrvalue = $(this).attr('data-category');
    
    if (attrvalue == "category-1") { 
        cat1.push(attrvalue);
    }
    
    if (attrvalue == "category-2") { 
        cat2.push(attrvalue); 
    }
    
    if (attrvalue == "category-3") { 
        cat3.push(attrvalue);
    } 
    });
    
    console.log('how many are here? :'+ cat1);
    console.log('how many are here? :'+ cat2);
    console.log('how many are here? :'+ cat3);
    
    0 讨论(0)
  • 2020-12-19 06:37

    May be dirty, but it's what I came up with:

    var iterate = 1;
    $('ul.categories li').each(function (i, v) {
        var thisCat = $(v).data('category');
        if ($('div#' + thisCat).length > 0) {
            $('div#' + thisCat).text(thisCat + ' (' + ++iterate + ')');
        } else {
            iterate = 1;
            var newDiv = '<div id="' + thisCat + '">' + thisCat + ' (1)</div>';
            $('#result').append(newDiv);
        }
    });
    

    And, fiddle.

    0 讨论(0)
  • 2020-12-19 06:39

    It is possible, check this jsFiddle I've created.

    var categories = {},
        category;
    
    $('.categories li[data-category]').each(function(i, el){
        category = $(el).data('category');
        if (categories.hasOwnProperty(category)) {
            categories[category] += 1;
        }
        else {
            categories[category] = 1;
        }
    });
    
    // print results
    for(var key in categories){
        console.log(key + ' (' + categories[key] + ')');
    }
    
    0 讨论(0)
提交回复
热议问题