I'm relatively new to OOP and want to create some simple HTML out of each object. Essentially, I have these objects:
const slides = {
"slide1": {
"image": "images/projects/authorising-engineers/winbledon-centre-court.jpg",
"caption": "Wimbledon Centre Court"
},
"slide2": {
"image": "images/projects/authorising-engineers/waterloo-train-station.jpg",
"caption": "Waterloo Train Station"
},
"slide3": {
"image": "images/projects/authorising-engineers/test.jpg",
"caption": "Das Lion"
},
"slide4": {
"image": "images/projects/authorising-engineers/some4.jpg",
"caption": "the four"
},
"slide5": {
"image": "images/projects/authorising-engineers/some5.jpg",
"caption": "no five"
}
}
And I just want the HTML to output this, where figure 1, 2 and 5 have unique classes:
"<figure class='individual-slide current'><img src='images/projects/authorising-engineers/winbledon-centre-court.jpg' style = 'background-image: url(images/projects/authorising-engineers/winbledon-centre-court.jpg)'><figcaption>Wimbledon Centre Court</figcaption></figure>"
"<figure class='individual-slide next'><img src='images/projects/authorising-engineers/waterloo-train-station.jpg' style = 'background-image: url(images/projects/authorising-engineers/waterloo-train-station.jpg)'><figcaption>Waterloo Train Station</figcaption></figure>"
"<figure class='individual-slide'><img src='images/projects/authorising-engineers/test.jpg' style = 'background-image: url(images/projects/authorising-engineers/test.jpg)'><figcaption>Das Lion</figcaption></figure>"
"<figure class='individual-slide'><img src='images/projects/authorising-engineers/some4.jpg' style = 'background-image: url(images/projects/authorising-engineers/some4.jpg)'><figcaption>the four</figcaption></figure>"
"<figure class='individual-slide previous'><img src='images/projects/authorising-engineers/some5.jpg' style = 'background-image: url(images/projects/authorising-engineers/some5.jpg)'><figcaption>no five</figcaption></figure>"
The closest I could achieve so far is the below - the following code successfully creates the HTML, but gives no control if I want to customise the nth item (i.e. adding classes to objects 1, 2 and 5):
// create the innerHtml
for (const slide in slides) {
let theHtml =
"<figure class='individual-slide'><img src='"
+ slides[slide].image
+ "' style = 'background-image: url("
+ slides[slide].image
+ ")'><figcaption>"
+ slides[slide].caption
+ "</figcaption></figure>";
}
Codepen of the above: https://codepen.io/ns91/pen/vYYZKKB
I've thus attempted to use a classic for loop so I can add if statements for the nth item (i.e. if i = 0, add the first class, etc), although this doesn't seem to work even without them:
// create the innerHtml
for (const i = 0; i < Object.keys(slides).length; i++) {
let theHtml =
"<figure class='individual-slide'><img src='"
+ slides[slides][i].image
+ "' style = 'background-image: url("
+ slides[slides[i].image
+ ")'><figcaption>"
+ slides[slides[i].caption
+ "</figcaption></figure>";
}
console.log(theHtml);
CodePen: https://codepen.io/ns91/pen/oNNwLzR
So my questions are:
- What's wrong with my code
- How can I loop through each object but add the 'current', 'next' and 'previous' classes
- And what's the best way of looping through objects like this?
Any advice would be much appreciated. Thanks for any help here
Object.entries(obj).map(([value, key], index) => {...generate html...}if you need key value and index