繁体   English   中英

整理出父子关系的数组,其中两个子共享一个父项,并使用div显示它

[英]Sorting out array of parent child relations where two children share a parent from an array and display it using div

我从neo4j查询中获取以下数据,发送回的数据格式为

home->parent->child1
home->parent->child2

home->parent2->child1

home->parent3->child1
home->parent3->child2
home->parent3->child3

我正在尝试使用javascript显示应该像这样的html

<div id="parent1">
  <div id="child1"></div>
  <div id="child2"></div>
</div>
<div id="parent2">
  <div id="child1"></div>
</div>

我尝试通过查询循环,并尝试使父级成为对象的索引,而子级成为其下的值

我会在php中这样做

$jsonContents = (object)("parent"=>"child","parent"=>"child"....);
$array = array();
foreach($jsonContents as $jsCo=>$jsoCont){
  $array[$jsoCont->parent][] = $jsoCont->child;
}

这将返回

$数组为

home->parent1->[0]->child
             ->[1]->child
      parent2->[0]->child...

这将使我避免检查home父类别的唯一性,并将它们放在层次结构中,以便我可以在MVC的View部分中正确解释它,以创建div结构。

这是示例json数据的网址

http://www.jsoneditoronline.org/?id=bdda268982eb431d361c25e9035bbc99

对此没有答案,请自行解决。 这里

var data = 'data shown in link above';
var myArr = [];

$.each(data, function(index, element) {
  var parent = String(element.parent.properties.name);
  var child = String(element.child.properties.name);

  if(myArr[parent]){
    myArr[parent][(myArr[parent].length)] = child;
  } else {
    myArr[parent] = Array(child);
  }

});

希望这对人们有所帮助。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM