Hello everyone,
I have a requirement for merging the PDFs together, I have a data table containing the two columns (Invoice and Ref Doc Number) and a list containing some PDFs having Ref Doc Number as prefix and I have to merge the invoice with the PDFs having corresponding Ref Doc Number as prefix.
CATCH : A invoice can have multiple Ref Doc Number and a Ref Doc Number can have multiple PDFs.
I have created the input string for the given code containing the data table and the PDF list
input_json = '{ "datatable" : [ { "Bill. Doc." : "1025294", "Ref. Doc." : "1022069792"}, { "Bill. Doc." : "54427960", "Ref. Doc." : "5750000117"}],
"pdf_list" : ["1022069792yoo","1022069792_test1","1022069792_test2","1022069792_test3","1022069792_test4","1022069792_test5","5750000117huugv","5750000117testtttt","5750000117_test11","5750000117_test2"]}'
and i want the output in such a way I can merge the invoice and the PDF as mentioned above
CODE : function generateInvoiceMapping(input_json) {
let data = JSON.parse(input_json);
let result = {};
data.datatable.forEach(entry => {
let invoice = entry["Bill. Doc."];
let refDoc = entry["Ref. Doc."];
if (!result[invoice]) {
result[invoice] = {};
}
let matchingPdfs = data.pdf_list.filter(pdf => pdf.startsWith(refDoc));
if (matchingPdfs.length > 0) {
result[invoice][refDoc] = matchingPdfs;
}
});
return JSON.stringify(result);
}
Output : {"1025294":{"1022069792":["1022069792yoo","1022069792_test1","1022069792_test2","1022069792_test3","1022069792_test4","1022069792_test5"]},"54427960":{"5750000117":["5750000117huugv","5750000117testtttt","5750000117_test11","5750000117_test2"]}}
ALWAYS GETTING OUTPUT AS ”Bot Error”, Although code is tunning fine in Visual Studio