I am trying to use if condition in for loop. But it is not working at conditional level
var CodeResponse = context.getVariable('AllCodeData');
var JSONDATA = JSON.parse(CodeResponse);
var Codes = JSONDATA[0].CODE;
context.setVariable('Code',Codes); // I am able to see code data
var flag1 = JSONDATA[1].FLAG;
context.setVariable('Flag',flag1); // I am able to see flag value
for(var i = 0; i<=JSONDATA.length; i++)
{
for(var j = 1; j<=JSONDATA.length; j++)
{
if (JSONDATA[i].FLAG == "N" && JSONDATA[j].FLAG == "N" && (JSONDATA[i].CODE === JSONDATA[j].CODE)) // I am not able read flag and code data into if condition
{
delete JSONDATA[i];
}
}
}
context.setVariable('CodeInfo', JSON.stringify(JSONDATA));
Data in AllCodeData :
[
{ "CODE": 1500, "FLAG": "N" },
{ "CODE": 1500, "FLAG": "Y" },
{ "CODE": 1500, "FLAG": "Y" }
]
While testing the above code I am getting below error
{ "fault": { "faultstring": "Execution of code failed with error: Javascript runtime error: \"TypeError: Cannot read property \"FLAG\" from undefined. (code.js:15)\"", "detail": { "errorcode": "steps.javascript.ScriptExecutionFailed" } } }
Can anyone please help me on this
Thanks,
Lakshmi.