Javascript 获取数组循环中父对象的名称
我有以下对象数组:Javascript 获取数组循环中父对象的名称,javascript,arrays,object,vue.js,Javascript,Arrays,Object,Vue.js,我有以下对象数组: payload: [ {name: one}, {name: two, values: {name: five} }, {name: three}, {name: four} ] 我以递归的方式循环这个过程,因为数据的深度可以随时改变。因此name:five可以再次拥有自己的值。 现在,当我循环一个对象的值时,我想要父对象的名称。所以对于name:five我想在一个方法中得到two 有没有办法得到这个名字 我使用vue.js作
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>
-
{{value.name}}:
-
{{value.type}}:
我在我的父文件中这样渲染这个循环:
<div class="row" v-for="value in payload.values">
<codeblock-value
:value="value">
</codeblock-value>
</div>
请记住,可以有多个具有值的对象。如果您可以稍微更改有效负载结构,将使生活变得更轻松
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]);
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);
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);
var有效载荷={
名称:“有效载荷”,
价值观:[{
姓名:“一”
}, {
姓名:“两个”,
价值观:[{
姓名:“五”
}]
}, {
姓名:“三”
}, {
姓名:“四”
}]
};
函数(父函数){
console.log(parent.name);
if(parent.hasOwnProperty('values')&&Array.isArray(parent.values)){
对于(var x=0,len=parent.values.length;x
VUE.JS的更新
同样,更改数据结构允许您访问父级。在本例中,我动态生成测试数据,以便每个子节点引用其父节点(我加入了一些随机性以生成文件夹或不生成文件夹)
数据生成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);
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);
var数据={
名字:'我的树',
儿童:[]
}
var-maxDepth=4;
函数createChild(父级,currentDepth){
var childrenValues=['hello','wat','test'];
var createChildFolderChance=0.5;
对于(变量x=0,len=childrenValues.length;x
更新的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);
}
},
function(){
if(此.isFolder){
this.open=!this.open
}否则{
有孩子的第一兄弟姐妹;
//在当前节点的父节点的子节点(即兄弟节点)之间循环,并返回具有子节点的第一个节点的名称
for(var x=0,len=this.model.parent.children.length;x
请添加您的代码。Vue网站提供了一个如何构建树视图的示例:是的,我知道,这是我代码的基础!但是现在,由于此树视图是基本代码,当我单击wat
或hello
时,我想记录子文件夹
。也许您可以看看这个。这是Vue.JS树视图。我用它作为我的基本代码。现在我想,当我点击wat
或hello
时,记录子文件夹
@InbarAzulay,并在我的回答中更新。虽然这个代码片段可以解决这个问题,但它确实有助于提高你文章的质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。还请尽量不要用解释性注释挤满你的代码,这会降低代码和解释的可读性!