I have a dynamically generated navigation structure shown. Each location has storetypes generated from the stores at the location.
I need to remove duplicates per location so that groceries can appear in both locations. I tried the common jquery solution below which removes duplicates but it results in each storetype appearing only in one location.
var seen = {};
$("ul#storetypes").find("li").each(function(index, html_obj) {
txt = $(this).text().toLowerCase();
if (seen[txt]) {
$(this).remove();
} else {
seen[txt] = true;
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="menu">
<li>Location 1
<ul id="storetypes" class="sub-menu">
<li>groceries</li>
<li>cakes</li>
<li>motor spares</li>
<li>groceries</li>
</ul>
</li>
<li>Location 2
<ul class="sub-menu">
<li>groceries</li>
<li>motor spares</li>
<li>motor spares</li>
<li>groceries</li>
</ul>
</li>
</ul>
3
Answers
what you did is correct, but just do it for every
<ul>
.You just need to target your top-level iteration at each sub-list.
Then iterate its children and remove duplicates.
Also, you don’t need jQuery.
You can do it like this:
This will look into each
.sub-menu
and see if any ‘li’ exist already with the same text, and remove the duplicated.Demo