在我的图表中,我有三种实体。 省,地区和城市。
关系是城市属于地区,而地区属于省。
这是我的数据库表 地区。
id | region_name | region_parent_id
1 | Western province | null
2 | Colombo | 1
3 | Gampaha | 1
4 | Kaluthara | 1
5 | Nugegoda | 2
6 | Maharagama | 2
7 | Central province | null
8 | Kandy | 7
9 | Mathale | 7
10 | theldeniya | 8
11 | penideniya | 8
如果我推导出关系,我会是这样,
西部省份
科伦坡
Nugegoda
Maharagama
Gampaha
Kaluthara
中部省份
Kandy
Theldeniya
Penideniya
数学
我以这样的对象数组的形式获取这些数据,
const regions = [
{id:1,region_name:"Western province",region_parent_id:null},
{id:2,region_name:"Colombo",region_parent_id:1},
{id:3,region_name:"Gampaha",region_parent_id:1},
{id:4,region_name:"Kaluthara",region_parent_id:1},
{id:5,region_name:"Nugegoda",region_parent_id:2},
{id:6,region_name:"Maharagama",region_parent_id:2},
{id:7,region_name:"Central province",region_parent_id:null},
{id:8,region_name:"Kandy",region_parent_id:7},
{id:9,region_name:"Mathale",region_parent_id:7},
{id:10,region_name:"theldeniya",region_parent_id:8},
{id:11,region_name:"penideniya",region_parent_id:8}
]
console.log(regions.filter(e=> !e.region_parent_id ))
我可以很容易地得到像这样的省份。
console.log(regions.filter(e=> !e.region_parent_id ))
但是如何获取地区列表和城市列表?
任何帮助!
非常感谢! =)
答案 0 :(得分:1)
尝试一下:
var parId = prompt("Enter region parent id : " ) ;
const regions = [
{id:1,region_name:"Western province",region_parent_id:null},
{id:2,region_name:"Colombo",region_parent_id:1},
{id:3,region_name:"Gampaha",region_parent_id:1},
{id:4,region_name:"Kaluthara",region_parent_id:1},
{id:5,region_name:"Nugegoda",region_parent_id:2},
{id:6,region_name:"Maharagama",region_parent_id:2},
{id:7,region_name:"Central province",region_parent_id:null},
{id:8,region_name:"Kandy",region_parent_id:7},
{id:9,region_name:"Mathale",region_parent_id:7},
{id:10,region_name:"theldeniya",region_parent_id:8},
{id:11,region_name:"penideniya",region_parent_id:8}
]
function belong(dis) {
var getProvince = regions.find( e => e.id === dis ) ;
if ( getProvince ) {
console.log("==========" + getProvince.region_name + "==========" ) ;
regions.map( c => {if (c.region_parent_id === dis ) console.log(c.region_name + " " + c.id +" Parent:(" + c.region_parent_id +")") } )
}
}
belong( parseInt(parId) );
答案 1 :(得分:1)
按以下方式使用过滤器,您将获得所需的结果。
const regions = [
{id:1,region_name:"Western province",region_parent_id:null},
{id:2,region_name:"Colombo",region_parent_id:1},
{id:3,region_name:"Gampaha",region_parent_id:1},
{id:4,region_name:"Kaluthara",region_parent_id:1},
{id:5,region_name:"Nugegoda",region_parent_id:2},
{id:6,region_name:"Maharagama",region_parent_id:2},
{id:7,region_name:"Central province",region_parent_id:null},
{id:8,region_name:"Kandy",region_parent_id:7},
{id:9,region_name:"Mathale",region_parent_id:7},
{id:10,region_name:"theldeniya",region_parent_id:8},
{id:11,region_name:"penideniya",region_parent_id:8}
];
var province = regions.filter(e => !e.region_parent_id);
province.forEach(p => {
p.district = regions.filter(e => e.region_parent_id == p.id);
// Uncomment following snippet if you want to have cities list inside district object also.
//p.district.forEach(d => {
// d.city = regions.filter(e => e.region_parent_id == d.id);
//});
var districtIds = p.district.map(d => d.id);
p.city = regions.filter(e => districtIds.includes(e.region_parent_id));
});
console.log(province);
答案 2 :(得分:1)
尝试一下:
const regions = [
{ id: 1, region_name: "Western province", region_parent_id: null },
{ id: 2, region_name: "Colombo", region_parent_id: 1 },
{ id: 3, region_name: "Gampaha", region_parent_id: 1 },
{ id: 4, region_name: "Kaluthara", region_parent_id: 1 },
{ id: 5, region_name: "Nugegoda", region_parent_id: 2 },
{ id: 6, region_name: "Maharagama", region_parent_id: 2 },
{ id: 7, region_name: "Central province", region_parent_id: null },
{ id: 8, region_name: "Kandy", region_parent_id: 7 },
{ id: 9, region_name: "Mathale", region_parent_id: 7 },
{ id: 10, region_name: "theldeniya", region_parent_id: 8 },
{ id: 11, region_name: "penideniya", region_parent_id: 8 }
];
const provinces = regions.filter(region => !region.region_parent_id);
for (let i = 0; i < provinces.length; i++) {
const currentProvince = provinces[i];
// get districts for this now
currentProvince.districts = regions.filter(region => region.region_parent_id === currentProvince.id);
for (let j = 0; j < provinces[i].districts.length; j++) {
const currentDistrict = provinces[i].districts[j];
// get cities for the district
currentDistrict.cities = regions.filter(region => region.region_parent_id === currentDistrict.id);
}
}
console.log(provinces)