I'm self-teaching myself vanilla JS and hope to get really good with it.
I've been racking my brain over this for several days. I'm trying to use template literals to display the properties of objects that are in an array.
Those objects are portfolio projects. In each object, I have a property that is an array of icon images. I'm able to get everything rendered on the page except for all of the icon images.
I can only get 1 icon image to display in my div with the class="skills-used".
Can someone help me figure out how to map through all of the images in that array of icons?
Alternatively, is there a better (or a recommended) way to do this instead?
const projects = [
{
image: "http://via.placeholder.com/360x180",
title: "Project 1",
skillIconImages: ["http://via.placeholder.com/50x50", "http://via.placeholder.com/55x50", "http://via.placeholder.com/50x55", "http://via.placeholder.com/60x50"],
description: "Ex rationibus deterruisset eos, ius tale nullam officiis an. Duo propriae mentitum salutandi id, nulla nobis persequeris ut eam, ex dicta libris laboramus duo. Amet errem voluptatibus an vix, eum ut nulla scriptorem. Ea etiam noluisse perfecto eum. Nec ad consul ubique, his id reque nonumy percipit."
},
{
image: "http://via.placeholder.com/360x180",
title: "Project 2",
skillIconImages: ["http://via.placeholder.com/50x50", "http://via.placeholder.com/55x50", "http://via.placeholder.com/50x55", "http://via.placeholder.com/60x50"],
description: "Ex rationibus deterruisset eos, ius tale nullam officiis an. Duo propriae mentitum salutandi id, nulla nobis persequeris ut eam, ex dicta libris laboramus duo. Amet errem voluptatibus an vix, eum ut nulla scriptorem. Ea etiam noluisse perfecto eum. Nec ad consul ubique, his id reque nonumy percipit."
},
{
image: "http://via.placeholder.com/360x180",
title: "Project 3",
skillIconImages: ["http://via.placeholder.com/50x50", "http://via.placeholder.com/55x50", "http://via.placeholder.com/50x55", "http://via.placeholder.com/60x50"],
description: "Ex rationibus deterruisset eos, ius tale nullam officiis an. Duo propriae mentitum salutandi id, nulla nobis persequeris ut eam, ex dicta libris laboramus duo. Amet errem voluptatibus an vix, eum ut nulla scriptorem. Ea etiam noluisse perfecto eum. Nec ad consul ubique, his id reque nonumy percipit."
}
];
Here is how I'm trying to render it to the page:
let portfolioItemsDiv = document.getElementById('portfolio-items');
projects.forEach((project) => {
portfolioItemsDiv.innerHTML += `
<div class="portfolio-item">
<img src="${project.image}" alt="${project.title}" />
<h3 class="project-title">${project.title}</h3>
<div class="skills-used">
${project.skillIconImages.map((x) => '<img src="' + x +'"')}
</div>
<div class="project-description">
<p>${project.description}</p>
</div>
</div>
`;
});
If the entire (non-refactored) function is needed, here it is:
(() => {
const projects = [
{
image: "http://via.placeholder.com/360x180",
title: "Project 1",
skillIconImages: ["http://via.placeholder.com/50x50", "http://via.placeholder.com/55x50", "http://via.placeholder.com/50x55", "http://via.placeholder.com/60x50"],
description: "Ex rationibus deterruisset eos, ius tale nullam officiis an. Duo propriae mentitum salutandi id, nulla nobis persequeris ut eam, ex dicta libris laboramus duo. Amet errem voluptatibus an vix, eum ut nulla scriptorem. Ea etiam noluisse perfecto eum. Nec ad consul ubique, his id reque nonumy percipit."
},
{
image: "http://via.placeholder.com/360x180",
title: "Project 2",
skillIconImages: ["http://via.placeholder.com/50x50", "http://via.placeholder.com/55x50", "http://via.placeholder.com/50x55", "http://via.placeholder.com/60x50"],
description: "Ex rationibus deterruisset eos, ius tale nullam officiis an. Duo propriae mentitum salutandi id, nulla nobis persequeris ut eam, ex dicta libris laboramus duo. Amet errem voluptatibus an vix, eum ut nulla scriptorem. Ea etiam noluisse perfecto eum. Nec ad consul ubique, his id reque nonumy percipit."
},
{
image: "http://via.placeholder.com/360x180",
title: "Project 3",
skillIconImages: ["http://via.placeholder.com/50x50", "http://via.placeholder.com/55x50", "http://via.placeholder.com/50x55", "http://via.placeholder.com/60x50"],
description: "Ex rationibus deterruisset eos, ius tale nullam officiis an. Duo propriae mentitum salutandi id, nulla nobis persequeris ut eam, ex dicta libris laboramus duo. Amet errem voluptatibus an vix, eum ut nulla scriptorem. Ea etiam noluisse perfecto eum. Nec ad consul ubique, his id reque nonumy percipit."
}
];
// get the DOM element to where all the portfolio items will go into
let portfolioItemsDiv = document.getElementById('portfolio-items');
// render each project to the DOM
projects.forEach((project) => {
portfolioItemsDiv.innerHTML += `
<div class="portfolio-item">
<img src="${project.image}" alt="${project.title}" />
<h3 class="project-title">${project.title}</h3>
<div class="skills-used">
${project.skillIconImages.map((x) => '<img src="' + x +'"')}
</div>
<div class="project-description">
<p>${project.description}</p>
</div>
</div>
`;
});
})();