I have an array containing a list of cars, and an image URL.
On the page, i have a list of lots of cars. I want to loop over all cars on page, and if it is within the cars array, then I want to swap it's image to the new one.
What would be the best way to go about this?
<!-- example of on page list of cars -->
<li class="car" data-id="car1">
<img src="https://imageurl.com/currentImage" alt="">
</li>
<li class="car" data-id="car12">
<img src="https://imageurl.com/currentImage" alt="">
</li>
<li class="car" data-id="car2">
<img src="https://imageurl.com/currentImage" alt="">
</li>
<li class="car" data-id="car7">
<img src="https://imageurl.com/currentImage" alt="">
</li>
--
const cars = [
{
"id": "car1"
"newImage": "imageaddress1"
},
{
"id": "car2"
"newImage": "imageaddress2"
},
{
"id": "car3"
"newImage": "imageaddress3"
},
]
const allCarsNodeList = document.querySelectorAll('.car');
const allCars = Array.from(allProductsNodeList);
First thoughts are to use filter to reduce the on page cars down to only the ones featured, but I'm lost on how to do the comparison since in neither the array or the on page data-attribute are directly there.
I'm confused basically! Any points in the right direction would be greatly appreciated.