Gungor Celik Gungor Celik - 7 months ago 9
Javascript Question

How can i do array of objects

I have html table with input.I want get value in inputbox

<table id="yeni">
<tr id="1">
<td>
<input id="1_1" style="width: 70px; font-size: 11px;">
</td>
<td>
<input id="1_2" style="width: 80px; font-size: 11px;">
</td>
<td>
<input id="1_3" style="width: 80px; font-size: 11px;">
</td>
<td>
<input id="1_4" style="width: 80px; font-size: 11px;">
</td>
<td>
<input id="1_5" style="width: 80px; font-size: 11px;">
</td>
<td>
<input id="1_6" style="width: 80px; font-size: 11px;">
</td>
<td>
<input id="1_7" style="width: 80px; font-size: 11px;">
</td>
</tr>
<tr id="2">
<td>
<input id="2_1" style="width: 70px; font-size: 11px;">
</td>
<td>
<input id="2_2" style="width: 80px; font-size: 11px;">
</td>
<td>
<input id="2_3" style="width: 80px; font-size: 11px;">
</td>
<td>
<input id="2_4" style="width: 80px; font-size: 11px;">
</td>
<td>
<input id="2_5" style="width: 80px; font-size: 11px;">
</td>
<td>
<input id="2_6" style="width: 80px; font-size: 11px;">
</td>
<td>
<input id="2_7" style="width: 80px; font-size: 11px;">
</td>
</tr>
<tr id="3">
<td>
<input id="3_1" style="width: 70px; font-size: 11px;">
</td>
<td>
<input id="3_2" style="width: 80px; font-size: 11px;">
</td>
<td>
<input id="3_3" style="width: 80px; font-size: 11px;">
</td>
<td>
<input id="3_4" style="width: 80px; font-size: 11px;">
</td>
<td>
<input id="3_5" style="width: 80px; font-size: 11px;">
</td>
<td>
<input id="3_6" style="width: 80px; font-size: 11px;">
</td>
<td>
<input id="3_7" style="width: 80px; font-size: 11px;">
</td>
</tr>
</table>


for every row ı want to create object

var stoktbl = {}
var table = [];

var rowCount = document.getElementById('yeni').rows.length;
for (var satirx = 1; satirx < rowCount; satirx++) {

stoktbl.firma_barkod = document.getElementById(satirx + "_" + "1").value;
stoktbl.adet = document.getElementById(satirx + "_" + "5").value;
stoktbl.birim_satis_fiyati = document.getElementById(satirx + "_" + "6").value;

table[satirx] = stoktbl;
}

var JsonString = JSON.stringify(table)
console.log(JsonString)


when run it with ;

I see only last row data repaet like

[null, {
"adet": "1",
"birim_satis_fiyati": "69.9",
"firma_barkod": "091986 "
}, {
"adet": "1",
"birim_satis_fiyati": "69.9",
"firma_barkod": "091986 "
}, {
"adet": "1",
"birim_satis_fiyati": "69.9",
"firma_barkod": "091986 "
}, {
"adet": "1",
"birim_satis_fiyati": "69.9",
"firma_barkod": "091986 "
}, {
"adet": "1",
"birim_satis_fiyati": "69.9",
"firma_barkod": "091986 "
}, {
"adet": "1",
"birim_satis_fiyati": "69.9",
"firma_barkod": "091986 "
}, {
"adet": "1",
"birim_satis_fiyati": "69.9",
"firma_barkod": "091986 "
}]

Answer

In your for-loop you need to re-initialize the stoktbl object

for(var satirx=1; satirx<rowCount; satirx++){
   stoktbl = {};
   //rest of the cpde
}

Otherwise, your table array will keep on using the same object at all the indexes.

Also, you need to use push method to add object to array

for(var satirx=1; satirx<rowCount; satirx++)
{
    stoktbl = {};
    stoktbl.firma_barkod=document.getElementById(satirx +"_"+"1").value;
    stoktbl.adet= document.getElementById(satirx +"_"+"5").value;
    stoktbl.birim_satis_fiyati= document.getElementById(satirx +"_"+"6").value;
    table.push( stoktbl );
}

This will ensure that first element of table array is not null.