我想从我目前的节点遍历数组去年父节点的方式如下:如何从当前记录向上遍历嵌套数组?
规则:
节点-1(以从文本数组属性STR从这里,其str不为null并将其推送到父数组属性中的currentText)
Node-1-1(从str的文本数组属性中获取str,其str不为null并将其推送到currentT在父数组属性EXT)
现在我所要做的是为节点1-1-1和节点1-1-1的currentText我想穿越这样的:
节点1-1-1 ===>节点1-1 ====>节点1:
1) Node-1-1-1 ===> Node-1-1
If(Node-1-1.text[0].str !=null)
currentText.parent.push(Node-1-1.text[0].str);
else if(Node-1-1.text[1].str!=null)
currentText.parent.push(Node-1-1.text[1].str);
else
//do nothing
2) Node-1-1 ====> Node-1
If(Node-1.text[0].str !=null)
currentText.parent.push(Node-1.text[0].str);
else if(Node-1.text[1].str!=null)
currentText.parent.push(Node-1.text[1].str);
else
//do nothing
所以最终为节点1-1-1和对节点的currentText -1-1-1,我应该有如下输出:
var currentText =
{
"str" : "This is my first Node-1-1-1 string",
"parent":[
{
"str" : ""This is my first Node-1 string"
},
{
"str" : ""This is my first Node-1-1 string"
}
]
}
注意:currentNode
变量将根据节点选择具有不同的节点对象。因此,如果用户选择节点Node-1-1-1,则currentNode将具有Node-1-1-1对象,并且如果用户选择了Node-1- 1个currentNode将具有节点1-1对象等
var records = [
{
"name": "Node-1",
"isParent": true,
"text" : [
{
"str" : "This is my first Node-1 string",
"parent":[]
},
{
"str" : "This is my second Node-1 string",
"parent":[]
}],
"nodes": [
{
"name": "Node-1-1",
"isParent": false,
"text" : [
{
"str" : "This is my first Node-1-1 string",
"parent":[]
},
{
"str" : "This is my second Node-1-1 string",
"parent":[]
}],
"nodes": [
{
"name": "Node-1-1-1",
"isParent": false,
"text" : [
{
"str" : "This is my first Node-1-1-1 string",
"parent":[]
},
{
"str" : "This is my second Node-1-1-1 string",
"parent":[]
}],
"nodes": []
}
]
}
]
}
]
var currentNode={
"name": "Node-1-1-1",
"isParent": false,
"text" : [
{
"str" : "This is my first Node-1-1-1 string",
"parent":[]
},
{
"str" : "This is my second Node-1-1-1 string",
"parent":[]
}],
"nodes": []
}
var currentText =
{
"str" : "This is my first Node-1-1-1 string",
"parent":[]
}
console.log(records);
为什么仅仅只有父节点的文本数组的第一个字符串? –
@NinaScholz因为我总是希望优先考虑所有父节点(节点1和节点1-1)的文本属性的第一条记录(如果str对于第一条记录不为空),但是如果它是空的然后取第二个文本属性的记录,但只有str不为null。 –