JQuery: counting a specific character in a table column

I have an HTML table in which each row has two columns. The first column is the capital letter, and the second is the brand name:

A | Amazon

A | Apple

B | BMW

C | Chanel

etc .. But whenever there are two brand names that have the same first capital letter, I would like the table to look like this:

A | Amazon

    | Apple

B | BMW

C | Chanel

In other words, if there are multiple instances of each capital letter, I would like to display only the first. If I applied the class to the first column, is there a way I could achieve this using jQuery?

+3
source share
2 answers

each() ( leftCol):

$(document).ready(function() {
    var lastLetter = "";
    $(".leftCol").each(function() {
        var $this = $(this);
        var text = $this.text();
        if (text != lastLetter) {
            lastLetter = text;
        } else {
            $this.text("");
        }
    });
});
+1

, , , "":

var previousLetter = null;
$('table td.letter').each(function(i, el){
    var currentLetter = el.innerHTML;
    if (currentLetter == previousLetter) el.innerHTML = '';
    previousLetter = currentLetter;
});

, $('table td:first-child')

, , $('#myTable td:first-child')

0

Source: https://habr.com/ru/post/1778985/


All Articles