enter code here
hello I would like to loop on a json file to create maps but I can’t find the loop I need, here is my obect json
type here javascript
let element=document.createElement`
<div class="grid-item-product">
<img src="${Produits.img}" alt="${Produits.id}">
<div class="card-content">
<button id="control-price-sucette">
<i class="fa-sharp fa-solid fa-pen"></i>
</button>
<h3>${title}</h3>
<span>A partir de <strong id="price-sucette">${price} €</strong> </span>
</div>
</div>
`
product.appendChild(element)
enter code hereexport const Produits= [
{
id:"sucette",
img:"./images/sucette.jpg",
title:"Sucette",
Price:25,
reduction:0,
},
{
id :"cannes",
img:"./images/canne.jpg",
title : "Cannes",
price : 25,
reduction:30
},
{
id:"oursons",
title:"Ourson",
img:"./image/ourson",
price:20,
reduction:0
},
{
id:"bonbon_durs",
title:"Bonbons durs",
img:"./images/dur.jpg",
price: 39,
reduction:25
},
{
id:"bonbon_acidulées",
title:"Bonbons acidulées",
img:"./image/acidul.jpg",
price:30,
reduction:0
},
{
id:"brochette",
title:"Brochette",
img:"./images/brochette.jpg",
price:30,
reduction:20
},
{
_id: "cake_balls",
get id() {
return this._id
},
set id(value) {
this._id = value
},
img:"./images/cake-ball.jpg",
title:"Cake bals",
price:40,
reduction:20
},
{
_id_1: "cake_pops",
get id() {
return this._id_1
},
set id(value) {
this._id_1 = value
},
title:"cake pops",
img:"./images/cake-pops.jpg",
price:50,
reduction:30
},
{
id: "cake_pops_coeur",
title:"cake pops coeur",
img:"./images/cake-pops-coeur",
price:50,
reduction:20
}
]
3
Answers
It is not quite clear from your question what you need.
Although i think you want to loop over the Items to get their attributes.
In this case i think this should work:
The best loop is Javascript’s
.map
function.Your HTML will need:
The
.map
function maps the array of products to an array of strings (not to a single long string) so you can use the.join
function to join the individual strings into one long one.Et voilà:
You can do the loop at that JSON using
.forEach
method. Try this: