While iterating an array I would like to display first two arrays item into first DIV and second two array items into second Div and so on. I have achieved my result partially, but still, I believe this is not correct. How do I achieve this?
I would like to display following arrays items under each div's as below:
First List: Pencil, 200 Rubber, 250 Second List Sharpner, 300 Pen, 400
Third List Paint, 500 Box, 50
<div class="container">
<div class="best order1" id="one">
<h1>First List</h1>
</div>
<div class="best order2" id="two">
<h1>Second List </h1>
</div>
<div class="best order3" id="three">
<h1>Third List</h1>
</div>
<input type="button" value="List" onclick="myArray();">
</div>
Below is my function;
function myArray() {
var arr1 = [];
arr1 = [{
"Item": "Pencil",
"Quantity": 200
},
{
"Item": "Rubber",
"Quantity": 250
},
{
"Item": "Sharpner",
"Quantity": 300
},
{
"Item": "Pen",
"Quantity": 400
},
{
"Item": "Paint",
"Quantity": 500
},
{
"Item": "Box",
"Quantity": 50
}
];
var portion = arr1.length / 2;
for (i = 0; i < arr1.length; i++) {
//alert(arr1[i].Item+','+arr1[i].Quantity );
if (i <= portion) {
document.getElementById('one').innerHTML += arr1[i].Item + ',' + arr1[i].Quantity + '<br>';
} else {
document.getElementById('two').innerHTML += arr1[i].Item + ',' + arr1[i].Quantity + '<br>';
}
}
}
This is my Fiddle