-2

I have a listing of events. I'd like to remove any events that have duplicate names and dates, but they can have different locations. I can successfully drop duplicates using a variation of code from this thread - JQuery: Remove duplicate elements? However I'd like to hide the location info for the first duplicate item and I can't seem to target that. Here is my code so far...

$('li').each(function() {
    var name = $(this).find('.name').text();
    var dates = $(this).find('.dates').text();
    var nameAndDates = name + dates;
    if (seen[nameAndDates]) {
        $(this).remove();
    }
    else {
        seen[nameAndDates] = true;
    }
});

Somewhere in there I need to target the first duplicate item and add something along the lines of...

$(this).find('.location').hide();

Any ideas?

Community
  • 1
  • 1

1 Answers1

0

count them and remove only if first duplicate

var seen = {};

$('li').each(function() {
    var name = $(this).find('.name').text();
    var dates = $(this).find('.dates').text();
    var nameAndDates = name + dates;
    if (nameAndDates in seen && seen[nameAndDates] === 1) {
        $(this).find('.location').hide();
    }

    seen[nameAndDates] = nameAndDates in seen ? seen[nameAndDates] + 1 : 1;
});
adeneo
  • 312,895
  • 29
  • 395
  • 388
  • I think his problem is removing the `.location` of the first duplicate item. – Fabrício Matté Dec 18 '13 at 18:06
  • @FabrícioMatté - wouldn't that be solved by just using the OP's code in the condition? Not sure I get it now? – adeneo Dec 18 '13 at 18:13
  • Exactly what Fabrico said – andrewhoule Dec 18 '13 at 18:13
  • @adeneo Yep I'm in the same boat as you. – Fabrício Matté Dec 18 '13 at 18:14
  • @andrewhoule - Isn't this what you're looking for? -> http://jsfiddle.net/dFtLM/1/ – adeneo Dec 18 '13 at 18:17
  • Oh I missed a condition, @adeneo that's what I thought. `=]` – Fabrício Matté Dec 18 '13 at 18:17
  • @adeneo - Not quite, though that gets me closer. That seems to just drop the location from the second duplicate. I need to drop all the duplicates but the first and on the first item also drop the location. – andrewhoule Dec 18 '13 at 18:22
  • @adeneo - There are still two events that say "Main Event Jan 25, 2014 • 8:00AM to 4:00PM" One with the location and one without. I need to drop that first with the location. – andrewhoule Dec 18 '13 at 18:27
  • But that's exactly what your original code does? Now I really don't get it? – adeneo Dec 18 '13 at 18:38
  • @adeneo - Sorry if I'm not being clear. The original markup produces a list like this - http://cl.ly/image/022Q330B3h16 I need to use jquery to remove duplicates (that duplicate name and dates) and on the event that stays also drop the location. So the end result in that example should look like this - http://cl.ly/image/1b1C1X1o1042 – andrewhoule Dec 18 '13 at 18:42
  • @andrewhoule - I think I got it in the last fiddle ? – adeneo Dec 18 '13 at 18:46