Answered by:
How to remove alternative blank row from JSON array..?

Question
-
User-1042970710 posted
I am reading my html table using javascript and store the value in javascript array everything works fine, I realize that it is automatically adding one extra row i.e { } alternatively
here it my javascript:
var paylist = new Array(); $("#EnterpriseItems tr").each(function () { var row = $(this); var pay = {}; pay.ConsolidationSetID = row.find("td").eq(0).html(); pay.ConsolidationSetDescription = row.find("td").eq(1).html(); pay.ElementCode = row.find("td").eq(2).html(); pay.Amount = row.find("td").eq(3).html(); pay.ElementClassification = row.find("td").eq(4).html(); paylist.push(pay); });
$('#PayElements').val(JSON.stringify(paylist)); //store array in to hidden value pay elements
var hv = $('#PayElements').val();
console.log(hv);Here is my Json with blank row added alternatively
[{},{"ConsolidationSetID":"مسير مكافأة مباشرة الأموال العامة","ConsolidationSetDescription":"123","ElementCode":"بدل ترحيل - نقل، بدل تهيؤ ونقل أمتعة","Amount":"1,000.00","ElementClassification":"مدفوع"},{},{"ConsolidationSetID":"مسير الإبتعاثات","ConsolidationSetDescription":"1233","ElementCode":"بدل انتداب - داخلي","Amount":"1,235.00","ElementClassification":"مدفوع"},{}]
Is there anyway I can eliminate the alternate blank rows { } added by javascript.??
Regards;
Imran Jalali.
Sunday, June 28, 2020 7:12 AM
Answers
-
User1535942433 posted
Hi jalali,
Accroding to your description and codes,if your blank row is the first or last row,you could use .not(':first') and .not(':last').
If you have other problems,you could post more details of your requirment and full codes of table to us.It will help us to solve your problem.
More details,you could refer to below codes:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(function () { var paylist = new Array(); $("#EnterpriseItems tr").not(':first').not(':last').each(function () { var row = $(this); var pay = {}; pay.ConsolidationSetID = row.find("td").eq(0).html(); pay.ConsolidationSetDescription = row.find("td").eq(1).html(); pay.ElementCode = row.find("td").eq(2).html(); pay.Amount = row.find("td").eq(3).html(); pay.ElementClassification = row.find("td").eq(4).html(); paylist.push(pay); }); $('#PayElements').val(JSON.stringify(paylist)); //store array in to hidden value pay elements var hv = $('#PayElements').val(); console.log(hv); }) </script> <table id="EnterpriseItems" runat="server"> <tr></tr> <tr> <td>A</td> <td>B</td> <td>C</td> <td>S</td> <td>D</td> </tr> <tr></tr> </table> <asp:TextBox ID="PayElements" runat="server"></asp:TextBox>
Result:
Best regards,
Yijing Sun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 29, 2020 5:21 AM
All replies
-
User1535942433 posted
Hi jalali,
Accroding to your description and codes,if your blank row is the first or last row,you could use .not(':first') and .not(':last').
If you have other problems,you could post more details of your requirment and full codes of table to us.It will help us to solve your problem.
More details,you could refer to below codes:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(function () { var paylist = new Array(); $("#EnterpriseItems tr").not(':first').not(':last').each(function () { var row = $(this); var pay = {}; pay.ConsolidationSetID = row.find("td").eq(0).html(); pay.ConsolidationSetDescription = row.find("td").eq(1).html(); pay.ElementCode = row.find("td").eq(2).html(); pay.Amount = row.find("td").eq(3).html(); pay.ElementClassification = row.find("td").eq(4).html(); paylist.push(pay); }); $('#PayElements').val(JSON.stringify(paylist)); //store array in to hidden value pay elements var hv = $('#PayElements').val(); console.log(hv); }) </script> <table id="EnterpriseItems" runat="server"> <tr></tr> <tr> <td>A</td> <td>B</td> <td>C</td> <td>S</td> <td>D</td> </tr> <tr></tr> </table> <asp:TextBox ID="PayElements" runat="server"></asp:TextBox>
Result:
Best regards,
Yijing Sun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 29, 2020 5:21 AM -
User-474980206 posted
or:
JSON.stringify(paylist.filter(paylist.filter(r => Object.keys(r).length)))
Monday, June 29, 2020 9:36 PM