如何从当前记录向上遍历嵌套数组?

时间:2017-08-25 07:36:08

标签: javascript

我想以下列方式遍历从当前节点到最后一个父节点的数组:

enter image description here

规则:

  • Node-1(从str的text数组属性获取str,其str为非null并将其推送到父数组属性中的currentText)

  • Node-1-1(从str的text数组属性获取str,其str为非null并将其推送到父数组属性中的currentText)

现在我要做的是Node-1-1-1和Node-1-1-1的currentText,我想这样遍历:

节点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和Node-1-1-1的currentText的最后,我应该有如下输出:

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将具有Node-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);




1 个答案:

答案 0 :(得分:1)

您可以使用迭代和递归方法,同时将父节点放入recusion调用以获取所需的字符串。

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

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

如果存在另一个node,则使用node属性和实际节点作为父节点再次调用该函数。



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>
&#13;
&#13;
&#13;

使用给定的节点名称,您可以使用迭代和递归方法来搜索五个节点名称并重新调整所有父节点信息。

&#13;
&#13;
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'));
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;