Is this possible to do using jQuery?
Lets say I have a list of items, with values attached eg:
- Item1: 5
- Item2: 10
- Item3: 20
- Item2: 15
- Item4: 30
- Item2: 25
- Item1: 11
How would I remove all duplicates and add the integers from removed duplicates to one entry, with an output as such using jQuery:
- Item1: 16
- Item2: 50
- Item3: 20
- Item4: 30
Edit: I’ve found a function to remove duplicates using $.each, but I have no idea where to go from here to store the values per iteration and modify the DOM for the original item.
var seen = {};
$('.variety_name').each(function() {
var txt = $(this).text();
if (seen[txt])
$(this).parent().remove();
else
seen[txt] = true;
});
Simple modification of the code you provided (with assumptions about the html):
When you haven’t seen an item, store the object in
seen. When you have seen it, add current item’s value to the stored object’s value, and delete the current item.Example: http://jsfiddle.net/JyGc9/