gpt4 book ai didi

javascript - 我怎样才能做对象数组

转载 作者:行者123 更新时间:2023-11-27 23:01:53 25 4
gpt4 key购买 nike

我有带有输入的 html 表。我想在输入框中获取值

<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>

对于我想创建对象的每一行

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)

当使用 ; 运行时

我只看到最后一行数据重复,例如

[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 "
}]

最佳答案

在 for 循环中,您需要重新初始化 stoktbl 对象

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

否则,您的table数组将继续在所有索引处使用相同的对象。

此外,您需要使用push方法将对象添加到数组

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 );
}

这将确保table数组的第一个元素不为空。

关于javascript - 我怎样才能做对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37000636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com