2016-11-23 45 views
-1

我有以下對象數組:以遞歸的方式,通過這個在陣列循環獲取父對象的名稱

payload: [ 
    {name: one}, 
    {name: two, 
    values: {name: five} 
    }, 
    {name: three}, 
    {name: four} 
] 

我環路,因爲這個深度的數據都可以隨時更改。因此name: five可以再次擁有自己的值。
現在當我遍歷一個對象的值時,我想要父對象的名稱。因此,對於name: five我想在方法中獲得two

有什麼方法可以獲得這個名字?

我使用vue.js一個Javascript庫。

這是我的循環:

<ul> 
    <div class="row"> 
     <li v-if="value.name" class="col-md-3 indent" @click="toggle"> 
      {{value.name}}: 
     </li> 
     <li v-else class="col-md-3 indent" @click="toggle"> 
      {{value.type}}: 
     </li> 
    </div> 
    <div v-show="open" v-if="isValue"> 
     <codeblock-value 
      v-for="value in value.values" 
      :value="value"> 
     </codeblock-value> 
    </div> 
</ul> 

我使這個循環像這樣在我的父文件:

<div class="row" v-for="value in payload.values"> 
    <codeblock-value 
     :value="value"> 
    </codeblock-value> 
</div> 

請記住,可以有與值的多個對象。

+1

請添加您的代碼。 –

+1

Vue網站提供瞭如何構建樹視圖的示例:https://vuejs.org/v2/examples/tree-view.html –

+0

是的,我知道,這是我的代碼的基礎!但現在,由於此樹視圖爲基本代碼,因爲我點擊'wat'或'hello',我想記錄'子文件夾'。 –

回答

0
function recurse(parentName, obj) { 
    console.log("Parent name is: " + parentName); 
    console.log("Obj name is: " + obj.name); 

    if(obj.values) { 
     recurse(obj.name, obj.values); 
    } 
} 

recurse(payload[1]); 
+4

儘管此代碼片段可能會解決問題,但[包括解釋](// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。也請儘量不要用解釋性註釋來擠佔代碼,這會降低代碼和解釋的可讀性! – kayess

0

如果你可以稍微改變你的有效載荷結構,它會讓生活變得更容易一些。

JSFIDDLE

JS

var payload = { 
    name: "payload", 
    values: [{ 
     name: "one" 
    }, { 
     name: "two", 
     values: [{ 
      name: "five" 
     }] 
    }, { 
     name: "three" 
    }, { 
     name: "four" 
    }] 
}; 

function dig(parent) { 
    console.log(parent.name); 
    if (parent.hasOwnProperty('values') && Array.isArray(parent.values)) { 
     for(var x = 0, len = parent.values.length; x < len; x++){ 
      dig(parent.values[x]); 
     } 
    } 
} 

dig(payload); 

更新VUE.JS 再一次,改變了數據結構允許用戶訪問父。在這個例子中,我動態生成測試數據,以便每個子節點引用其父(我投擲一些隨機性生成文件夾或不)。

JSFIDDLE

數據生成JS

var data = { 
    name: 'My Tree', 
    children: [] 
} 
var maxDepth = 4; 
function createChild(parent, currentDepth){ 
    var childrenValues = ['hello', 'wat', 'test']; 
    var createChildFolderChance = 0.5; 
    for(var x = 0, len = childrenValues.length; x < len; x++){ 
     var child = { 
      name: childrenValues[x], 
      parent: parent 
     } 
     if(Math.random() < createChildFolderChance && currentDepth < maxDepth){ 
      child.children = []; 
      currentDepth++; 
      createChild(child, currentDepth) 
     } 
     parent.children.push(child); 
    } 
} 

createChild(data, 0); 

更新Vue.JS單擊代碼

function() { 
    if (this.isFolder) { 
     this.open = !this.open 
    }else{ 
     var firstSiblingWithChildren; 
     // cycle through current node's parent's children (ie. siblings) and return the name of the first node that has children 
     for(var x = 0, len = this.model.parent.children.length; x < len; x++){ 
      if(this.model.parent.children[x].hasOwnProperty('children') && Array.isArray(this.model.parent.children[x].children)){ 
       firstSiblingWithChildren = this.model.parent.children[x].name; 
       break; 
      } 
     } 
     console.log(firstSiblingWithChildren); 
    } 
}, 
+0

也許你可以看看這個[JSFiddle](https://jsfiddle.net/yyx990803/3p0j5sgy/?utm_source=website&utm_medium=embed&utm_campaign=3p0j5sgy)。這是Vue.JS樹視圖。我用這個作爲我的基本代碼。現在我想,當我點擊'wat'或'hello'時,記錄'子文件夾'。 –

+0

@InbarAzulay,更新了https://jsfiddle.net/3p0j5sgy/87/並在我的回答中 – haxxxton