87,990
社区成员
发帖
与我相关
我的任务
分享
[{
"text": "中华人民共和国",
"spid": "2013010535",
"nodes": [{
"text": "东北",
"spid": "2013010535",
"nodes": [{
"text": "辽宁省",
"spid": "2013035210",
"nodes": [{
"text": "沈阳市",
"spid": "2013035211",
"nodes": [{
"text": "沈阳市和平区",
"spid": "2013038441"
}, {
"text": "沈河区",
"spid": "2013038441"
}, {
"text": "大东区",
"spid": "2013038441"
}, {
"text": "皇姑区",
"spid": "2013038441"
}, {
"text": "沈阳市铁西区",
"spid": "2013038441"
}, {
"text": "苏家屯区",
"spid": "2013038441"
}, {
"text": "东陵区",
"spid": "2013038441"
}, {
"text": "沈北新区",
"spid": "2013038441"
}, {
"text": "于洪区",
"spid": "2013038441"
}, {
"text": "辽中县",
"spid": "2013038441"
}, {
"text": "康平县",
"spid": "2013038441"
}, {
"text": "法库县",
"spid": "2013038441"
}, {
"text": "新民市",
"spid": "2013038441"
}]
}, {
"text": "大连市",
"spid": "2013035211",
"nodes": [{
"text": "中山区",
"spid": "2013038441"
}, {
"text": "西岗区",
"spid": "2013038441"
}, {
"text": "沙河口区",
"spid": "2013038441"
}, {
"text": "甘井子区",
"spid": "2013038441"
}, {
"text": "旅顺口区",
"spid": "2013038441"
}, {
"text": "金州区",
"spid": "2013038441"
}, {
"text": "长海县",
"spid": "2013038441"
}, {
"text": "瓦房店市",
"spid": "2013038441"
}, {
"text": "普兰店市",
"spid": "2013038441"
}, {
"text": "庄河市",
"spid": "2013038441"
}]
}]
}]
}]
}]
function merge (map1, map2) {
let result = [], tmp1 = JSON.parse(JSON.stringify(map1)), tmp2 = JSON.parse(JSON.stringify(map2));
tmp1.forEach(function (item1) {
let flag = false;
tmp2.forEach(function (item2) {
if (item1.nodeid === item2.nodeid) {
flag = true;
result.push({
name: item1.name,
nodeid: item1.nodeid,
nodes: merge(item1.nodes, item2.nodes)
});
}
});
if (!flag) result.push(item1);
});
tmp2.forEach(function (item2) {
let flag = false;
tmp1.forEach(function (item1) {
if (item1.nodeid === item2.nodeid) flag = true;
});
if (!flag) result.push(item2);
});
return result;
}
function up(nodeid, map) {
var currentMap = null;
map.forEach(function (subMap) {
if (subMap.nodeid == nodeid) currentMap = subMap;
});
if (currentMap !== null) {
return currentMap;
}
else {
var result = null;
map.forEach(function (subMap) {
if (subMap.hasOwnProperty("nodes")) {
var subRe = up(nodeid, subMap.nodes);
if (subRe != null) {
subMap.nodes = subRe;
result = subMap;
}
}
});
return result;
}
}
var res1 = up('133619', json);
var res2 = up('133992', json);
json.aspx:73 Uncaught TypeError: map.forEach is not a function
at up (json.aspx:73)
at json.aspx:83
at Array.forEach (<anonymous>)
at up (json.aspx:81)
at Object.success (json.aspx:60)
at fire (jquery.js:2913)
at Object.fireWith [as resolveWith] (jquery.js:3025)
at done (jquery.js:7400)
at XMLHttpRequest.<anonymous> (jquery.js:7822)
function up(nodeid, map) {
let currentMap = null, tmp = JSON.parse(JSON.stringify(map));
tmp.forEach(function (subMap) {
if (subMap.nodeid === nodeid) currentMap = subMap;
});
if (currentMap !== null) {
return [currentMap];
} else {
let result = null;
tmp.forEach(function (subMap) {
if (subMap.hasOwnProperty("nodes")) {
let subRe = up(nodeid, subMap.nodes);
if (subRe !== null) subMap.nodes = [subRe]; result = subMap;
}
});
return [result];
}
}
var json = $.parseJSON(response);
var json2 = json;
console.log(json);
var res1 = up('133619', json);
console.log(json2);
var json = $.parseJSON(response);
console.log(json); //查询前
var res1 = up('133619', json);
console.log(json); //查询后
var json = $.parseJSON(response);
var json2 = json;
console.log(json);
var res1 = up('133619', json);
console.log(json2);
function up(nodeid, map) {
var currentMap = null;
map.forEach(function (subMap) {
if (subMap.nodeid == nodeid) currentMap = subMap;
});
if (currentMap !== null) {
return currentMap;
}
else {
var result = null;
map.forEach(function (subMap) {
if (subMap.hasOwnProperty("nodes")) {
var subRe = up(nodeid, subMap.nodes);
if (subRe != null) {
subMap.nodes = subRe;
result = subMap;
}
}
});
return result;
}
}
var res1 = up('133619', json);
var res2 = up('133992', json);
json.aspx:73 Uncaught TypeError: map.forEach is not a function
at up (json.aspx:73)
at json.aspx:83
at Array.forEach (<anonymous>)
at up (json.aspx:81)
at Object.success (json.aspx:60)
at fire (jquery.js:2913)
at Object.fireWith [as resolveWith] (jquery.js:3025)
at done (jquery.js:7400)
at XMLHttpRequest.<anonymous> (jquery.js:7822)


function up(nodeid, map) {
let currentMap = null;
map.forEach(function (subMap) {
if (subMap.nodeid === nodeid) currentMap = subMap;
});
if (currentMap !== null) {
return [currentMap];
} else {
let result = null;
map.forEach(function (subMap) {
if (subMap.hasOwnProperty("nodes")) {
let subRe = up(nodeid, subMap.nodes);
if (subRe !== null) subMap.nodes = [subRe]; result = subMap;
}
});
return [result];
}
}
for (var i = 0; i < regions.length; i++) {
/*
遍历各个地区,打印结果示例
console.log(regions[i]);
{text: "东北", nodeid: "137697", spid: "2013358041", nodes: Array(3)}
*/
var provinces = regions[i].nodes;
for (var j = 0; j < provinces.length; j++) {
/*
遍历各个省,打印结果示例
console.log(provinces[j]);
{text: "辽宁省", nodeid: "133596", spid: "2013035210", nodes: Array(14)}
*/
var cities = provinces[j].nodes;
for (var k = 0; k < cities.length; k++) {
/*
遍历各个市,打印结果示例
console.log(cities[k]);
{text: "沈阳市", nodeid: "133693", spid: "2013035211", nodes: Array(13)}
*/
var couties = cities[k].nodes;
for (var m = 0; m < couties.length; m++) {
/*
打印各个县、区,打印结果示例
console.log(couties[m]);
{text: "沈阳市和平区", nodeid: "134494", spid: "2013038441"}
*/
}
}
}