[英]how to loop over JSON array having different key value pairs to display specific values depending on condition in html using javascript
Suppose my JSON array is as shown:假设我的 JSON 数组如图所示:
[{
"type": "Sweatshirt",
"brand": "H&M",
"Color": "Yellow",
"Neck": "Turtle neck",
"Arm": "Full Arm"
},
{
"type": "Sweatshirt",
"brand": "PUMA",
"Color": "Black",
"Neck": "Round neck",
"Arm": "Full Arm"
},
{
"type": "Dresses",
"Length": "knee length",
"Occasion": "Party",
"Neck": "Halter neck"
},
{
"type": "Dresses",
"Length": "Floor length",
"Occasion": "Casual",
"Neck": "Boat neck"
}
]
how can I loop over array and check if the type
is dress or SweatShirt and display respective values in HTML div
如何遍历数组并检查type
是连衣裙还是 SweatShirt 并在 HTML div
中显示相应的值
Here is an example that will show you how you can loop and assign values to html elements这是一个示例,将向您展示如何循环并为 html 元素分配值
This example is for demonstration only此示例仅用于演示
array=[{ "type": "Sweatshirt", "brand": "H&M", "Color": "Yellow", "Neck": "Turtle neck", "Arm": "Full Arm" }, { "type": "Sweatshirt", "brand": "PUMA", "Color": "Black", "Neck": "Round neck", "Arm": "Full Arm" }, { "type": "Dresses", "Length": "knee length", "Occasion": "Party", "Neck": "Halter neck" }, { "type": "Dresses", "Length": "Floor length", "Occasion": "Casual", "Neck": "Boat neck" }] disp=document.getElementById("disp") array.forEach(o=>{ if(o.type=="Sweatshirt"){ type=document.createElement("li") brand=document.createElement("li") type.textContent=o.type brand.textContent=o.brand disp.appendChild(type) disp.appendChild(brand) }})
<div id="disp7"><ul id="disp"></ul></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.