我在嵌套for循環中打印2D數組時遇到問題。我在javascript中創建了對象,方法,數組和對象數組,現在我想通過使用for循環和2D數組來顯示值。我嘗試了我所知道的方法,但沒有按照我的預期得到輸出。誰能幫幫我嗎 ?Javascript:用於循環打印輸出2D數組的嵌套
code:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title> Trial </title>
<script type="text/javascript">
function item(item_name, item_taste, item_description) {
this.name = item_name;
this.taste = item_taste;
this.description = item_description;
this.display = displayClass;
}
function displayClass(){
document.documentElement.className = 'js';
document.write(this.name);
document.write(this.taste);
document.write(this.item_description);
document.write('<br />');
}
var myItem = new item("Pani Puri", "Spicy", "Crispy shells, Potatoes & Onions Served with sweet & chilled Tangy mintwater");
var myItem2 = new item("Aloo Tikki", "Spicy", "2 Whole Wheat Fried Breads Served With Potato Curry");
var myItem3 = new item("Papri chat", "Spicy", "Homemade chips, lentil dumplings, garbanzo bean, potato yogurt, chutney, onion & spices");
var myItem4 = new item("Chicken Palak", "Meduim Spicy", "Boneless Chicken Cooked In Hot Spicy Sauce With Potatoes");
var myItem5 = new item ("Chicken Korma", "Meduim Spicy", "Chicken In A Mild Creamy Sauce & Cashew Nuts");
var myItem6 = new item("Chicken Masala", "Spicy", "Bite Sized Chicken Cooked In An Onion Based Sauce");
var myItem7 = new item("Kheer", "Sweet", " Indian Rice Pudding Spiced With Cardamom");
var myItem8 = new item("Rasmalai", "Sweet", "Made From Homemade Style Fresh Cheese In Sweetned Milk");
var myItem9 = new item("Gulab Jamun", "Sweet", "Sweet Round Shaped Pastry Fried & Soaked In Flavored Syrup");
property = new Array("Varieties", "Taste", "Item Description");
category = new Array("Chaats", "Chicken", "Dessert");
chaats = new Array(myItem, myItem2, myItem3);
chicken = new Array(myItem4, myItem5, myItem6);
dessert = new Array(myItem7, myItem8, myItem9);
var cat = new Array(chaats, chicken, dessert);
</script>
</head>
<body>
<table border="4", align="center", cellpadding="5", cellspacing="5" >
<thead>
<tr>
<script type="text/javascript">
for (i = 0; i < 3; i++) {
document.write('<th>'+ property[i] +'</th>'); }
</script>
</tr>
</thead>
<tbody>
<tr>
<script type="text/javascript">
for (j = 0; j < 3; j++) {
document.write("<th><tr>" + category[j] + "</tr></th>");
for (k = 0; k < 3; k++) {
document.write("<td>" + cat[j][k] + "</td>");
}
}
</script>
</tr>
</tbody>
</table>
</body>
</html>
預期輸出:
輸出:
它不是'」