Answer the question
In order to leave comments, you need to log in
How to remove duplicate paragraphs with jQuery?
Hey everyone, I urgently need some help from jQuery wizards:
I have a script that iterates over a list, calculates the first letter in an element of the list, and appends it to that element.
$.each(listitems, function (idx, item) {
item = $(item);
var firstLetter = $(item).find('a').text().charAt(0);
$(item).append("<p>"+firstLetter+"</p>").addClass(firstLetter);
});
Answer the question
In order to leave comments, you need to log in
var listitems = $(".works__item")
var usedLetters = []; // Инициализируем массив для учета использованных букв
$.each(listitems, function (idx, item) {
item = $(item);
var firstLetter = $(item).find('a').text().charAt(0);
if (usedLetters.indexOf(firstLetter) === -1) { // Продолжаем работать с буквой только в случае, если она еще не использовалась
$(item).append("<p>"+firstLetter+"</p>").addClass(firstLetter);
usedLetters.push(firstLetter); // После обработки помещаем букву в массив использованных букв
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question