Question

JavaScript Code Issue

  • 22 April 2024
  • 2 replies
  • 16 views

Badge

Greetings,

I am trying to take this data:

{"schedMaint":[{"itemName":"ELT Battery Replacement","infoURL":"","due":"2024-09-30","remaining":"162 days","color":"Green"},{"itemName":"Transponder","infoURL":"","due":"2025-03-31","remaining":"344 days","color":"Green"},{"itemName":"Altimeter\/Static Inspection","infoURL":"","due":"2025-03-31","remaining":"344 days","color":"Green"},{"itemName":"Registration","infoURL":"","due":"2027-09-30","remaining":"1257 days","color":"Green"},{"itemName":"ELT Inspection","infoURL":"","due":"2025-03-31","remaining":"344 days","color":"Green"},{"itemName":"AD 2001-23-03 Forward Door Post","infoURL":"http:\/\/rgl.faa.gov\/Regulatory_and_Guidance_Library\/rgAD.nsf\/AOCADSearch\/28C538CC707E1A6086256B090060BC0C?OpenDocument","due":"2025-03-31","remaining":"344 days","color":"Green"},{"itemName":"AD 2011-10-09 Seat Rails","infoURL":"http:\/\/rgl.faa.gov\/Regulatory_and_Guidance_Library\/rgAD.nsf\/AOCADSearch\/144A3898BF71041A8625788F004B5DF2?OpenDocument","due":"3162.1 hrs","remaining":"29.10 hrs","color":"Green"},{"itemName":"Oil Change (50-Hr)","infoURL":"","due":"3166.1 hrs","remaining":"33.10 hrs","color":"Green"},{"itemName":"AD 76-07-12 R1 Ignition Switches","infoURL":"http:\/\/rgl.faa.gov\/Regulatory_and_Guidance_Library\/rgAD.nsf\/AOCADSearch\/C14960A415D956BD86256E520053A53E?OpenDocument","due":"3216.1 hrs","remaining":"83.10 hrs","color":"Green"}]}

and basically convert to an HTML list using this:

// Parse the JSON data from inputData.schedMaint to an object

const maintData = JSON.parse(inputData.schedMaint);

// Extract the array of maintenance items from the parsed object

const maintItems = maintData.schedMaint;

// Initialize an empty string to hold the table data

let tableData = "";

// Loop through each maintenance item

for (let i = 0; i <maintItems.length; i++) {

// Replace the escaped forward slashes in the infoURL with forward slashes

const infoURL =maintItems[i].infoURL.replace(/\//g, "/");

// Add a row to the tableData string with the item name, info URL, due date, and remaining time

tableData += "<li>" + maintItems[i].itemName + " Due: <b>" +maintItems[i].due + "</b> <ul> <li>Remaining: <b>" + maintItems[i].remaining + "</b></ul>"; }

// Set the output to an object with a property of tableData that holds the generated table

output = { tableData: tableData };

 

It has been working until recently, and I have no idea why as we haven’t changed anything. I’m getting this error: SyntaxError: Unexpected token ')'

I’m not a code guy so I’m not sure what exactly I’m looking for to try to fix it.


2 replies

Userlevel 7
Badge +14

Hi @SARGuy 

One option to try is to provide ChatGPT with the code and input, and prompt it to troubleshoot.
 

 

Badge

I was able to get the code mostly working, however, my lack of RegEx knowledge/skill is causing one last issue. I’m trying to extract any items in the code that start with “AD” and end with a “)” I have this JS, but it returns no results, even though there is at least one item in the JSON:

 

// Parse the JSON data from inputData.schedMaint to an object const maintData = JSON.parse(inputData.schedMaint);

// Extract the array of maintenance items from the parsed object const maintItems = maintData.schedMaint; var target = '(/AD.*\)/)'

// Initialize an empty string to hold the table data

let tableData = "";

for (let i = 0; i <maintItems.length; i++) {

  if (maintItems[i].itemName === target){

  const infoURL =maintItems[i].infoURL.replace(/\//g, "/"); tableData += "<li><a href=" + maintItems[i].infoURL +">" + maintItems[i].itemName + "</a> | Due: <b>" +maintItems[i].due + "</b> <ul> <li>Remaining: <b><font color=" + maintItems[i].color +">" + maintItems[i].remaining + "</b></ul></font>"; }

}

// Set the output to an object with a property of tableData that holds the generated table output = { tableData: tableData };

Reply