我有一个数组包含一些随机的城市名称,另一个json对象数组,它包含各个城市的源和目的地,但是这些单个城市的路径数组不在一个序列中。我需要按照他们访问的顺序制作它,我无法制作逻辑..请帮助..
城市数组(动态大小):
var CITIES = ["PARIS","LONDON","DELHI","SINGAPORE","DUBAI"]
路径的Json数组(在随机序列中):
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"}
]};

答案 0 :(得分: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));

答案 1 :(得分:0)
你可以尝试使用表单链接列表。第一个节点将包含PREVIOUS_CITY为null的城市,最后一个节点将使NEXT_CITY为null ..然后在链接列表中对它们进行Arraging之后...你可以轻松地打印它们..
第一节点迪拜 - >第二届巴黎 - >第3伦敦 - >第四德里 - >第五届新加坡
如果您需要检查上一个城市..您可以使用双向链表.. 有一个资源告诉您如何在JS中实现链接列表
答案 2 :(得分:0)
谢谢@TedHopp ......这不是有效的,而是在做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))
答案 3 :(得分:0)
试试这个 $就({
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);
}
}
});