Click here to Skip to main content
15,911,848 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
question must look confusing I have one array of Data I am looping through this data
in for loop inside forEach loop Actually I am importing Data from CSV file and than I want to create a XML file from this Data my code is below which can Help in understanding question
JavaScript
var array=[
    {
        "Date": "20-10-21",
        "Narration": "Payment To Party1",
        "DebitAccount0": "Party3",
        "DebitAmount0": "25,000.00",
        "DebitAccount1": "Party1",
        "DebitAmount0": "25,000.00",
        "CreditAccount0": " HDFC BANK ",
        "CreditAmount0": "50,000.00"
    },
    {
        "Date": "19-10-21",
        "Narration": "Payment To Party2",
        "DebitAccount0": "Party1",
        "DebitAmount0": "30,000.00",
        "DebitAccount1": "Party2",
        "DebitAmount1": "30,000.00",
        "CreditAccount0": " HDFC BANK ",
        "CreditAmount0": "60,000.00"
    },
    {
        "Date": "18-10-21",
        "Narration": "Payment To Party3",
        "DebitAccount0": "Party 2",
        "DebitAmount0": "12,500.00",
        "DebitAccount1": "Party 3",
        "DebitAmount1": "12,500.00",
        "CreditAccount0": " HDFC BANK ",
        "CreditAmount0": "25,000.00"
    }
];

var XMLfile="<ENVELOPE>"+"<HEADER>"+"<TALLYREQUEST>Import Data</TALLYREQUEST>"+"</HEADER>"+"<BODY>"+"<IMPORTDATA>"+"<REQUESTDESC></REQUESTDESC>"+"<REQUESTDATA>"+"<TALLYMESSAGE>";

array.forEach((array,index)=>{
XMLfile+="<VOUCHER>"+"<DATE>"+array.Date+"</DATE>"+"<NARRATION>"+array.Narration+"</NARRATION>"+"<VOUCHERTYPENAME>"+voucherType+"</VOUCHERTYPENAME>"+"<VOUCHERNUMBER></VOUCHERNUMBER>";

for(i=0;i<debitcount;i++)//debitcount variable stores number of debit account entry like 1,2,3 in this case it is 2{
XMLfile+="<ALLLEDGERENTRIES.LIST>"+"<LEDGERNAME>"+array.DebitAccount+[j]+"</LEDGERNAME>"+"<ISDEEMEDPOSITIVE>Yes</ISDEEMEDPOSITIVE>"+"<AMOUNT>"+"-"+array.DebitAmount[j]+"</AMOUNT>"
+"</ALLLEDGERENTRIES.LIST>";}

for(j=0;j<creditcount;j++)//similar to debit count in this case 1 {
XMLfile+="<ALLLEDGERENTRIES.LIST>"+"<LEDGERNAME>"+array.CreditAccount+[j]+"</LEDGERNAME>"+"<ISDEEMEDPOSITIVE>No</ISDEEMEDPOSITIVE>"+"<AMOUNT>"+array.CreditAmount+[j]+"</AMOUNT>"+"</ALLLEDGERENTRIES.LIST>"+"</VOUCHER>";
}

});


What I have tried:

If I try
array.CreditAccount0
than it is sawing correct answer but if I try
array.CreditAccount+[j]
it is sawing result as undefined
Posted
Updated 22-Oct-21 6:29am

eval("array.DebitAccount"+i.toString())
worked for me I am looping through array where there is object DebitAccount0,DebitAccount1,DebitAccount2 and can be many now I want to add value of these object to XML file where this line of code
var XMLfile="some xml code"
will start my XMl file than after this line I want add Data From array so I have used forEach loop here so loop will add three lines of data here as there is 3 objects
now inside these three array object there is some object with name DebitAccount0,DebitAccount1 here if only one debit account is there then I want add only one line of xml code + array data if two debit account is there like DebitAccount0,DebitAccount1 than I want add two line of data there for I have added a for loop here which can loop for these debit account and similar things for Credit Account
 
Share this answer
 
Comments
Richard Deeming 22-Oct-21 12:27pm    
eval is almost never the correct solution. :)
It is not clear what you are trying to do but the correct syntax is:
JavaScript
array[j].CreditAccount0;
 
Share this answer
 
The correct syntax for Javascript is:
Javscript
array["CreditAccount" + j]
or
JavaScript
array[`CreditAccount${j}`]
Property accessors - JavaScript | MDN[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900