So lets say I have a Javascript Array of Product Objects
function Product() {
this.Name = "";
this.Cost = "";
this.Color = "";
}
var ProductsArray = [];
var product1 = new Product();
product1.Name = "Name1";
product1.Cost = "Cost1";
product1.Color = "Color1";
var product2 = new Product();
product2.Name = "Name2";
product2.Cost = "Cost2";
product2.Color = "Color2";
var product3 = new Product();
product3.Name = "Name3";
product3.Cost = "Cost3";
product3.Color = "Color3";
ProductsArray.push(product1);
ProductsArray.push(product2);
ProductsArray.push(product3);
ProductsArray now has the following
product1: [Name1, Cost1, Color1],
product2: [Name2, Cost2, Color2],
product3: [Name3, Cost3, Color3]
How do I loop through this Array of Objects so taht I'm able to create table dynamically through jQuery which as the following format:
It's sort of like a table whose columns are horizontal
+-----------------------------------+
| Names | Name 1 | Name 2 | Name 3 |
+-----------------------------------+
| Costs | Cost 1 | Cost 2 | Cost 3 |
+-----------------------------------+
| Colors | Color1 | Color2 | Color3 |
+-----------------------------------+
<table>
<tr>
<th>Names</th>
<td>Name1</>
<td>Name2</>
<td>Name3</>
</tr>
<tr>
<th>Costs</th>
<td>Cost1</>
<td>Cost2</>
<td>Cost3</>
</tr>
<tr>
<th>Colors</th>
<td>Color1</>
<td>Color2</>
<td>Color3</>
</tr>
<table>
I guess the problem I'm having is since I loop through the Array in this fashion:
for (var i = 0; i < ProductsArray.length; i++) {
ProductsArray[i].Name
ProductsArray[i].Cost
ProductsArray[i].Color
}
I can only go through one object at time so I need a way to go through the values of each section each iteration, for example I need to go through the values of each Name and put them in <td>
columns, then go through the values of each Cost and put them in a <td>
etc...