JavaScript过滤出类别

时间:2019-01-11 05:33:34

标签: javascript

我有一个对象数组。

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("PROVINCES",regions.filter(e=> !e.region_parent_id));

// get districts
let province_ids = []
regions.forEach(e=>{
  if(!e.region_parent_id){
    province_ids.push(e.id)
  }
})

console.log("PROVINCE IDS",province_ids)

regions.forEach(e=>{
  if(province_ids.includes(e.region_parent_id)){
    console.log(e.region_name,"IS A DISTRICT")
  }
})

这些对象可以分为3种类型。

  1. 地区
  2. 城市

关系是城市属于一个地区,地区属于一个省。

我可以很容易地得到省份或地区。

你能告诉我如何获得城市吗?

任何帮助!

预先感谢=)

4 个答案:

答案 0 :(得分:2)

您可以像这样使用Array.prototype.some()

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(r => r.region_parent_id == null);
const districts = regions.filter(r => provinces.some(p => p.id === r.region_parent_id));
const cities = regions.filter(r => districts.some(d => d.id === r.region_parent_id));

console.log("Provinces: ", provinces);
console.log("Districts: ", districts);
console.log("Cities: ", cities);

不是可以肯定的最有效方法,但是只要您的区域列表相对较短,就不会造成任何问题。

答案 1 :(得分:1)

您可以从顶层开始,并为每个子级别过滤阵列。每次保存一组ID,以便您可以通过在固定时间内在父集中查找孩子的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}]
    
let provinces = regions.filter(e => !e.region_parent_id)
let province_id = new Set(provinces.map(p => p.id))

let region = regions.filter(e => province_id.has(e.region_parent_id))
let region_id = new Set(region.map(r => r.id))

let cities = regions.filter(r => region_id.has(r.region_parent_id))

console.log("provinces:", provinces)
console.log("regions:", region)
console.log("cities:", cities)

答案 2 :(得分:1)

console.log("CITIES",regions.filter(e=> (e.region_parent_id && regions.find(x=> x.id==e.region_parent_id).region_parent_id!=null)));

返回

CITIES 
(4) [{…}, {…}, {…}, {…}]
0: {id: 5, region_name: "Nugegoda", region_parent_id: 2}
1: {id: 6, region_name: "Maharagama", region_parent_id: 2}
2: {id: 10, region_name: "theldeniya", region_parent_id: 8}
3: {id: 11, region_name: "penideniya", region_parent_id: 8}`

PS-不过,您的情况很简单。过滤掉省和地区后,父阵列中的其余所有城市。无需复杂的JS查询。

答案 3 :(得分: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}

];

function find(obj,index=0){
  if(obj.region_parent_id){return find(regions.filter(r=>r.id==obj.region_parent_id)[0],++index);} return index;
}

regions.forEach(r=>{
	let index=find(r);	
	if(index==0){console.log(`Province ${r.region_name}`);}if(index==1){console.log(`District ${r.region_name}`);}if(index==2){console.log(`City ${r.region_name}`);}
});