I am encountering a challenge with this for loop. My goal is to extract the most recent order of "customers" and save it in my database. However, running this loop fetches both the failed order and the recent order.
for (var i = 0; i < json.length; i++) {
var TestOrdersXML = <testOrders _key="@testOrderId" operation="insertOrUpdate"/>;
if (json[i].testOrderId != undefined) TestOrdersXML.@testOrderId = json[i].testOrderId;
if (json[i].customerId != undefined) TestOrdersXML.@customerId = json[i].customerId;
if (json[i].status != undefined) TestOrdersXML.@status = json[i].status;
if (json[i].installationOrderData.state != undefined) TestOrdersXML.@state = json[i].installationOrderData.state;
logInfo("Status: " + json[i].status + " STATE: " + json[i].installationOrderData.state);
//collection.appendChild(TestOrdersXML);
}
The log:
Status: FAILED State: failed
Stauts: SUCCESS State: BOOKED
This array consists of two objects.
[
{
"installationOrderData":{
"state": "booked"
},
"customerId": 123456,
"testOrderId": 123456,
"status": SUCCESS
},
{
"installationOrderData":{
"state": "failed"
},
"customerId": 123456,
"testOrderId": 123456,
"status": FAILED
}
]
The question I need assistance with is how can I specifically retrieve only the most recent object?
Thank you.