簡體   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