在同一对象中搜索相关属性。 JavaScript的

时间:2016-11-10 13:35:26

标签: javascript json ecmascript-6 javascript-objects

我需要帮助根据父ID属性匹配相同对象中的子项....

JSON:

{
"1": {
"id": 1,
"name": "My Crib",
"type": "Home",
"order": 0,
"parent": null
},
"2": {
"id": 2,
"name": "First floor",
"type": "Floor",
"order": 1,
"parent": {
"id": 1,
"url": "http://localhost:8080/rest/areas/1"
}
},
"3": {
"id": 3,
"name": "Garage",
"type": "Garage",
"order": 2,
"parent": {
"id": 1,
"url": "http://localhost:8080/rest/areas/1"
}
},
"4": {
"id": 4,
"name": "Garden",
"type": "Garden",
"order": 3,
"parent": {
"id": 1,
"url": "http://localhost:8080/rest/areas/1"
}
},
"5": {
"id": 5,
"name": "Entrance hall",
"type": "Entrance",
"order": 1,
"parent": {
"id": 2,
"url": "http://localhost:8080/rest/areas/2"
}
},
"6": {
"id": 6,
"name": "Kitchen",
"type": "Kitchen",
"order": 2,
"parent": {
"id": 2,
"url": "http://localhost:8080/rest/areas/2"
}
},
"7": {
"id": 7,
"name": "Living room",
"type": "LivingRoom",
"order": 3,
"parent": {
"id": 2,
"url": "http://localhost:8080/rest/areas/2"
}
},
"8": {
"id": 8,
"name": "Dog house",
"type": "DogHouse",
"order": 1,
"parent": {
"id": 4,
"url": "http://localhost:8080/rest/areas/4"
}
}
}

const mappedAreas = Object.keys(areas).map(function(key) {
  const area = areas[key];
  const wrappers = statics["test.area.groups"]["wrappers"];
  const leafs = statics["test.area.groups"]["leafs"];

  if (wrappers.indexOf(area["type"]) > -1) {
      //In the array!

      //If not a home
      if(area["type"] != "Home"){
        console.log("floor:")
        console.log(area)

        //get wrapper childrens
        const children = Object.keys(areas).map(function(key) {
          const child = areas[key];

          if(child["type"] != "Home"){
            if(child["parent"]["id"] == area["id"] && leafs.indexOf(child["type"]) > -1){
              console.log(child);
              //return <Area key={"area_index_"+key} areaData={area} />
            }
          }
        });
        console.log("endfloor/")
        //return <Area key={"area_index_"+key} areaData={area} />
      }
  } else {
      //Not in the array

  }
});

有没有办法比在另一张地图中放置一张地图更好?

基本上我将所有区域都放在一个对象中,我希望将它们与彼此匹配,寻找[&#34;父母&#34;] [&#34; id&#34;]以匹配[&#34 ; ID&#34]。

告诉我你是否需要更多描述。

0 个答案:

没有答案