I have two arrays that I am using to simulate two different REST call's JSON response for the time with some static data. One to get the available active categories and another to get all links and then search to find what links go with what category.
The goal here is to build out a navigation that will take each category and display the corresponding links underneath each.
Currently I am unable to get the category to display only once and above the links related to each category then draw the output to the dom once complete.
I tried to use $.one but that did not work. Does anyone have any pointers or suggestions to nudge me in the right direction?
var links = [
{
"__metadata": {
"id": "",
"uri": "http://www.whatever.com"
},
"Id": 01,
"Title": "Link 01 - test category 01",
"Link": "https://www.google.com",
"Category": {
"__metadata": {
"type": ""
},
"results": [
"Test Category 01"
]
},
"Image": null,
"ID": 01
},
{
"__metadata": {
"id": "",
"uri": "http://www.whatever.com",
"etag": "",
"type": ""
},
"Id": 02,
"Title": "Link 02 - test category 01",
"Link": "https://www.google.com",
"Category": {
"__metadata": {
"type": ""
},
"results": [
"Test Category 01"
]
},
"Image": null,
"ID": 02
},
{
"__metadata": {
"id": "",
"uri": "http://www.whatever.com",
"etag": "",
"type": ""
},
"Id": 03,
"Title": "Link 01 - test category 02",
"Link": "https://www.google.com",
"Category": {
"__metadata": {
"type": ""
},
"results": [
"Test Category 02"
]
},
"Image": null,
"ID": 209
},
{
"__metadata": {
"id": "",
"uri": "http://www.whatever.com",
"etag": "",
"type": ""
},
"Id": 210,
"Title": "Link 02 - test category 02",
"Link": "https://www.somerandomdomain.com",
"Category": {
"__metadata": {
"type": ""
},
"results": [
"Test Category 02"
]
},
"Image": null,
"ID": 210
}
]
//category arr
var categoryArr = [
"Test Category 01",
"Test Category 02",
"Test Category 03"
]
var categoryTitle;
var menu = $("#output2");
$.each(categoryArr, function (catIndex, category) {
$.each(links, function(linkIndex, links) {
if(links.Category.results == category) {
// DOM ELEMENTS
var item = $('<div>').addClass('navContainer'),
title = $('<h4>'),
info = $('<p>'),
link = $('<a>');
// CATEGORY TITLE
info.text(categoryArr[catIndex]); // not showing once per iteration.
// ADD LINKS
link.attr('href',links.Link)
.text(links.Title)
.appendTo(title);
// ADD EVERYTHING
item.append(info,title);
// DISPLAY TO CONTAINER
item.appendTo(menu);
}
})// end glinks
}) // end categoryArr
.navContainer {
border: 1px solid grey;
margin: 10px;
padding:5px;
}
.links ul li {
list-style-type:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="output2"></div>
<hr>
<!-- result should be like this -->
<h5>
Result should look like this below
</h5>
<div class="navContainer">
<div class="title">Category title</div>
<div class="links">
<ul>
<li><a href="#">http://www.google.com</a></li>
<li><a href="#">http://www.google.com</a></li>
<li><a href="#">http://www.google.com</a></li>
</ul>
</div>
</div>
</div>
<div class="navContainer">
<div class="title">Category title</div>
<div class="links">
<ul>
<li><a href="#">http://www.google.com</a></li>
<li><a href="#">http://www.google.com</a></li>
<li><a href="#">http://www.google.com</a></li>
</ul>
</div>
</div>
</div>
etc.. etc..