1

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"}
]};

1
  • 1
    Well, it seems like the entry that has an empty PREVIOUS_CITY property needs to be first. Then you can simply chain them together based on NEXT_CITY. (If the data might have cycles, that's a harder problem, but I don't see that here.) Commented Jun 22, 2016 at 5:39

4 Answers 4

1

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":""}
    ]
};

var map = {};
var point = '';
CITY_ROUTE.ROUTE.forEach(r => {
    map[r.CURRENT_CITY] = r;
    if (!r.PREVIOUS_CITY) {
        point = r.CURRENT_CITY;
    }
});

CITY_ROUTE.ROUTE = [];
while(point) {
    CITY_ROUTE.ROUTE.push(map[point]);
    point = map[point].NEXT_CITY;
}
console.log(JSON.stringify(CITY_ROUTE));

Sign up to request clarification or add additional context in comments.

Comments

0

Well you could try something of form Linked List.. The first node will contain the city having PREVIOUS_CITY null and the last node will have the NEXT_CITY null.. then after Arraging them in a linked list.. you could easily print them..

1st node Dubai -> 2nd Paris -> 3rd London -> 4th Delhi -> 5th Singapore

In case you need to check for the previous city.. you can use doubly linked list.. There is a resource telling you how to implement linked lists in JS

http://code.tutsplus.com/articles/data-structures-with-javascript-singly-linked-list-and-doubly-linked-list--cms-23392

Comments

0

Thanks @TedHopp ... It's not efficient but doing the JOB ..

var SortedArray=[];
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":""}
]};

for(var index in CITY_ROUTE.ROUTE)
{
	if(CITY_ROUTE.ROUTE[index].PREVIOUS_CITY == "")
  {
  var NextCity = CITY_ROUTE.ROUTE[index].NEXT_CITY;
  SortedArray.push(CITY_ROUTE.ROUTE[index])
  CITY_ROUTE.ROUTE.splice(index,1);
    while(CITY_ROUTE.ROUTE.length>0)
    {
    		for(var i in CITY_ROUTE.ROUTE)
				{
        	if(CITY_ROUTE.ROUTE[i].CURRENT_CITY === NextCity)
          {
          SortedArray.push(CITY_ROUTE.ROUTE[i])
          NextCity = CITY_ROUTE.ROUTE[i].NEXT_CITY;
          CITY_ROUTE.ROUTE.splice(i,1);
          }
        }			
    }
  }
}
console.log(SortedArray.length)
console.log(CITY_ROUTE.ROUTE.push(SortedArray))
console.log(JSON.stringify(CITY_ROUTE.ROUTE))

Comments

0

Try this $.ajax({

type: "POST",
url: "<?php print $this->Url ?>index.php/employee/addemployee/viewrole,//your URL

data: {'CITY_ROUTE': CITY_ROUTE,'CITY_ROUTE_IN_SEQUENCE',CITY_ROUTE_IN_SEQUENCE},
success: function (response) {
var data = jQuery.parseJSON(response);

}

                                                                        }
                                                                    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.