2017-08-25 93 views
2

我想从我目前的节点遍历数组去年父节点的方式如下:如何从当前记录向上遍历嵌套数组?

enter image description here

规则:

  • 节点-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);

+0

为什么仅仅只有父节点的文本数组的第一个字符串? –

+0

@NinaScholz因为我总是希望优先考虑所有父节点(节点1和节点1-1)的文本属性的第一条记录(如果str对于第一条记录不为空),但是如果它是空的然后取第二个文本属性的记录,但只有str不为null。 –

回答

1

同时考虑父节点到recusion呼吁得到想要的字符串你可以使用迭代和递归方法。

基本上它迭代实际的数组,并检查是否从调用给出父。

如果给定,则父属性将获得父项的父项和直接父项的更新。

如果存在另一个node,则函数将再次以node propery和实际节点作为父项进行调用。

function setParent(array, parent) { 
 
    array.forEach(function (o) { 
 
     parent && o.text.forEach(function (p) { 
 
      p.parent = p.parent.concat(parent.text[0].parent); 
 
      p.parent.push({ str: parent.text[0].str }); 
 
     }); 
 
     o.nodes && setParent(o.nodes, o); 
 
    }); 
 
} 
 

 
var data = [{ 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: [] }] }] }]; 
 

 
setParent(data); 
 

 
document.getElementById('out').innerHTML = JSON.stringify(data, 0, 4);
<pre id="out"></pre>

随着节点的给定的名称,你可以使用迭代和递归方法以及对fiven节点名称后,搜索和重新调整所有的父节点信息。

function getString(array) { 
 
    var string; 
 
    return array.some(function (o) { 
 
     return string = o.str; 
 
    }) && string || ''; 
 
} 
 

 
function getParent(array, node, parents) { 
 
    var result; 
 
    return array.some(function (o) { 
 
     if (o.name === node) { 
 
      result = parents; 
 
      return true; 
 
     } 
 
     return o.nodes && (result = getParent(o.nodes, node, (parents || []).concat(getString(o.text)))); 
 
    }) && result || undefined; 
 
} 
 

 
var data = [{ 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: [] }] }] }]; 
 

 
console.log(getParent(data, 'foo')); 
 
console.log(getParent(data, 'Node-1')); 
 
console.log(getParent(data, 'Node-1-1')); 
 
console.log(getParent(data, 'Node-1-1-1'));
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

这工作。非常感谢你的帮助我的努力。赞赏:) –