I have something that works, taking the liberty of adding a class called "city" to some of the li
elements to help differentiate them.
JSFiddle here
It loops through and finds other anchors share the same text but do not reference the same element. It then finds the courses in their lists and adds them to the original anchor's list. I think it could be made clearer by adding additional classes, but the general concept is there.
One thing I hadn't originally counted on was needing to make sure that the elements are still visible. I grab the array of anchors up top, and they're still referenced even after having been removed.
$(function() {
$('li.city a').each(function(ix) {
if ($(this).is(':visible'))
{
var $anchor = $(this);
var myText = $(this).text();
var $matches = $('a').filter(function() {
return (($(this).text() == myText) && (this !== $anchor.get(0)));
});
$.each($matches, function() {
var $snip = $(this).parent().find('ul.courses li');
// add the li elements to the first anchor's list
$anchor.parent().find('ul.courses').append($snip);
// bubble up to duplicate anchor's ul tag and remove it entirely
$(this).closest('li.city').parent().remove();
});
}
});
});