concat对象属性与它的父属性

时间:2017-11-29 13:05:43

标签: javascript

我有一个对象数组,其中每个对象都有一个子数组,在这个子数组中,我有其他对象,也有一个子数组,依此类推,如下所示:

[
      {
        "id": "10036",
        "text": "A",
        "children": [
          {
            "id": "10020",
            "text": "B",
            "children": [
              {
                "id": "10030",
                "text": "C",
                "children": []
              },
              {
                "id": "10034",
                "text": "D",
                "children": []
              }
            ]
          },
          {
            "id": "10034",
            "text": "D",
            "children": []
          },
          {
            "id": "10026",
            "text": "E",
            "children": []
          }
        ]
      },
      {
        "id": "10024",
        "text": "F",
        "children": [
          {
            "id": "10020",
            "text": "B",
            "children": [
              {
                "id": "10030",
                "text": "C",
                "children": []
              },
              {
                "id": "10034",
                "text": "D",
                "children": []
              }
            ]
          },
          {
            "id": "10032",
            "text": "G,
            "children": []
          }
        ]
      }
    ]

因此对于children数组中的每个对象,我想用它的父id属性来连接它的id属性,所以结果应该是这样的:

[
      {
        "id": "10036",
        "text": "A",
        "children": [
          {
            "id": "10036_10020",
            "text": "B",
            "children": [
              {
                "id": "10036_10020_10030",
                "text": "C",
                "children": []
              },
...

我该怎么做?

2 个答案:

答案 0 :(得分:1)

尝试这种递归方法

function process(arr, parentId)
{        
    parentId = parentId ? "_" + parentId : ""; //if parentId is passed then prefix the delimiter _ with it
    return (arr || []).map( function(item){
       item.id += parentId; //append the parentId
       item.children = process( item.children, item.id ); //invoke the method if the children are there
       return item;
    })
}
process(arr);

<强>演示

&#13;
&#13;
var arr = [{
    "id": "10036",
    "text": "A",
    "children": [{
        "id": "10020",
        "text": "B",
        "children": [{
            "id": "10030",
            "text": "C",
            "children": []
          },
          {
            "id": "10034",
            "text": "D",
            "children": []
          }
        ]
      },
      {
        "id": "10034",
        "text": "D",
        "children": []
      },
      {
        "id": "10026",
        "text": "E",
        "children": []
      }
    ]
  },
  {
    "id": "10024",
    "text": "F",
    "children": [{
        "id": "10020",
        "text": "B",
        "children": [{
            "id": "10030",
            "text": "C",
            "children": []
          },
          {
            "id": "10034",
            "text": "D",
            "children": []
          }
        ]
      },
      {
        "id": "10032",
        "text": "G",
        "children": []
      }
    ]
  }
];

function process(arr, parentId) {
  parentId = parentId ? "_" + parentId : "";
  return (arr || []).map(function(item) {
    item.id += parentId; //append the parentId
    item.children = process(item.children, item.id); //invoke the method if the children are there
    return item;
  })
}
console.log(process(arr));
&#13;
&#13;
&#13;

答案 1 :(得分:0)

要做你想要的,你需要使用递归函数(https://www.sitepoint.com/recursion-functional-javascript/),请查看代码片段示例。

var array = [
    {
        "id": "10036",
        "text": "A",
        "children": [
            {
                "id": "10020",
                "text": "B",
                "children": [
                    {
                        "id": "10030",
                        "text": "C",
                        "children": []
                    },
                    {
                        "id": "10034",
                        "text": "D",
                        "children": []
                    }
                ]
            },
            {
                "id": "10034",
                "text": "D",
                "children": []
            },
            {
                "id": "10026",
                "text": "E",
                "children": []
            }
        ]
    },
    {
        "id": "10024",
        "text": "F",
        "children": [
            {
                "id": "10020",
                "text": "B",
                "children": [
                    {
                        "id": "10030",
                        "text": "C",
                        "children": []
                    },
                    {
                        "id": "10034",
                        "text": "D",
                        "children": []
                    }
                ]
            },
            {
                "id": "10032",
                "text": "G",
                "children": []
            }
        ]
    }
];

const concatId = (items) => {
    for(var i in items)
        concatIdRecursive(items[i]);
};

const concatIdRecursive = (item) => {
    if (item.hasOwnProperty('children')) {

        for (var i in item.children) {
            var child = item.children[i];

            if (child){
                child.id = item.id + "_" + child.id;
                concatIdRecursive(child);
            }
        }
    }
};
concatId(array);

alert(JSON.stringify(array));

希望你能帮忙。