gpt4 book ai didi

javascript - 用js和foreach为元素腾出一段时间

转载 作者:行者123 更新时间:2023-11-29 09:46:41 24 4
gpt4 key购买 nike

我有一个 json 对象(由服务器返回),我想用一段时间或 forEach 来显示购物车的产品。我的对象的名称是response2,它包含购物车的产品。我想花点时间在表格中显示这样的产品(只是想法):

<div>
<p>${response2.product}</p>
<p>${response2.price}</p>
</div>

抱歉英语不好,编程经验低!谢谢!

这是该对象的外观:

[  
{
"id":46,
"email":"rr",
"product":"Tort Oreo",
"description":"Detalii: (ex: textul de pe tort, unde sa fie plasate figurinele si in ce numar)",
"figurina":"",
"img_tort":"nu exista",
"price":"299"
},
{
"id":47,
"email":"rr",
"product":"Pizza Nevada",
"description":"Personalizare pizza",
"figurina":"",
"img_tort":"",
"price":"25"
},
{
"id":48,
"email":"rr",
"product":"Pizza Nevada22",
"description":"Personalizare pizza",
"figurina":"",
"img_tort":"",
"price":"29"
}
]

最佳答案

JSON response2 是一个对象数组。

您需要引用数组索引,然后才能调用/访问对象元素。

var response2 = [  
{
"id":46,
"email":"rr",
"product":"Tort Oreo",
"description":"Detalii: (ex: textul de pe tort, unde sa fie plasate figurinele si in ce numar)",
"figurina":"",
"img_tort":"nu exista",
"price":"299"
},
{
"id":47,
"email":"rr",
"product":"Pizza Nevada",
"description":"Personalizare pizza",
"figurina":"",
"img_tort":"",
"price":"25"
},
{
"id":48,
"email":"rr",
"product":"Pizza Nevada22",
"description":"Personalizare pizza",
"figurina":"",
"img_tort":"",
"price":"29"
}
];

window.onload = function() {
var i, max = response2.length;
for(i=0;i<max;i++) {
console.log("product:["+ response2[i].product +"], price:["+ response2[i].price +"]");
}
}

关于javascript - 用js和foreach为元素腾出一段时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55533084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com