I have a array consist some random city names , Another Array of json object which consist the Source and Destination of Individual cities , But this route array of individual cities are not in a sequence. I need to make it in a sequence in order they visited , I am unable to make the logic .. please help ..
Array of Cities (Dynamic Size):
var CITIES = ["PARIS","LONDON","DELHI","SINGAPORE","DUBAI"]
Json Array of Route (In Random Sequence) :
var CITY_ROUTE = {"ROUTE":[
{"CURRENT_CITY":"PARIS","NEXT_CITY":"LONDON","PREVIOUS_CITY":"DUBAI",},
{"CURRENT_CITY":"DELHI","NEXT_CITY":"SINGAPORE","PREVIOUS_CITY":"LONDON"},
{"CURRENT_CITY":"LONDON","NEXT_CITY":"DELHI","PREVIOUS_CITY":"PARIS"},
{"CURRENT_CITY":"SINGAPORE","NEXT_CITY":"","PREVIOUS_CITY":"DELHI"},
{"CURRENT_CITY":"DUBAI","NEXT_CITY":"PARIS","PREVIOUS_CITY":""}
]};
/Required to Rearange the Json Object in Visiting sequence where Starting City will have null PREVIOUS_CITY value and Last City will have null NEXT_CITY Value/
var CITY_ROUTE_IN_SEQUENCE = {"ROUTE":[
{"CURRENT_CITY":"DUBAI","NEXT_CITY":"PARIS","PREVIOUS_CITY":""}
{"CURRENT_CITY":"PARIS","NEXT_CITY":"LONDON","PREVIOUS_CITY":"DUBAI",},
{"CURRENT_CITY":"LONDON","NEXT_CITY":"DELHI","PREVIOUS_CITY":"PARIS"},
{"CURRENT_CITY":"DELHI","NEXT_CITY":"SINGAPORE","PREVIOUS_CITY":"LONDON"},
{"CURRENT_CITY":"SINGAPORE","NEXT_CITY":"","PREVIOUS_CITY":"DELHI"}
]};
PREVIOUS_CITYproperty needs to be first. Then you can simply chain them together based onNEXT_CITY. (If the data might have cycles, that's a harder problem, but I don't see that here.)