I am trying to loop through 2 arrays. The first array is the link name the second array is the links 'a' value. I want to loop through the two arrays attaching the value of the second array to the href of each link that is created / in the first array. This is what I have and it not working for me.
const links = ['Home', 'Contact', 'About'];
const hrefLinks = ['/', 'contact', 'about'];
for (let i = 0; i < links.length; i++) {
for (let j = 0; j < hrefLinks.length; i++) {
if (links.length === hrefLinks.length) {
const li = document.createElement('li');
const liLink = document.createElement('a');
liLink.setAttribute('href', hrefLinks[i]);
liLink.className = 'Test-Class';
li.appendChild(liLink);
li.className = 'nav-link';
list.appendChild(li);
li.innerHTML += links[i];
}
}
}
I do have it working with one forEach loop but got confused on how I would nest the second forEach;
const links = ['Home', 'Contact', 'About'];
const hrefLinks = ['/', 'contact', 'about'];
links.forEach(function (link) {
const li = document.createElement('li');
const liLink = document.createElement('a');
li.appendChild(liLink);
li.className = 'nav-link';
list.appendChild(li);
li.innerHTML += link;
});
Is this the proper way of doing this or is there an easier / cleaner way of doing this?