m0_46549277
m0_46549277
2021-04-28 18:30
采纳率: 81.8%
浏览 150

vue.js数组中有多个子级,怎么把他们全部拼接到单个数组同一级中

[
    {
      "id": 1,
      "title": "1",
      "children": [
        {
          "id": 2,
          "title": "1.1",
        },
        {
          "id": 3,
          "title": "1.2",
        },
        {
          "id": 4,
          "title": "1.3",
          "children": [],
        },
        {
          "id": 5,
          "title": "1.4",
					"children": [
            {
              "id": 6,
          		"title": "1.4.1",
              "children": [],
            },
              {
              "id": 7,
          		"title": "1.4.2",
            }
          ],
        },
        {
          "id": 8,
          "title": "1.5",
        }
      ]
    },
    {
      "id": 2,
      "title": "2",
      "children": [],
    }
  ],
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • liuhan4890
    已采纳

     item.children.length > 0改为item.children && item.children.length > 0即可,仅判断item.children.length > 0可能会存在item.children不存在,导致item.children.length报错undifined。基本就是雨生百谷老铁的代码 

    export function FlatArray (data) {
      console.log('原数组长度:', data.length)
      if (data.length === 0) {
        return []
      }
      let arr = []
      data.map((item) => {
        if (item.children && item.children.length > 0) {
          arr = arr.concat(FlatArray(item.children))
        } else {
          item.children = []
        }
        arr.push(item)
      })
      console.log('打平后数组长度:', arr.length)
      return arr
    }
    
    点赞 2 评论
  • weixin_44058725

    就是给拉平呗

     //扁平化数组(tree)变为一维数组
          FlatArray = (data, val) => {
                if (data.length == 0) {
                      return [];
                }
                let arr = [];
                console.log(data,"00")
                data.map((item) => {
                      
                      if (item.children.length > 0) {
                            arr = arr.concat(this.FlatArray(item.children));
                      }else{
                            item.children=[]
                      }
                      arr.push(item);
                })
                return arr;
          }
          let data = [
                {
                      "id": 1,
                      "title": "1",
                      "children": [
                            {
                                  "id": 2,
                                  "title": "1.1",
                                  "children": [],
                            },
                            {
                                  "id": 3,
                                  "title": "1.2",
                                  "children": [],
                            },
                            {
                                  "id": 4,
                                  "title": "1.3",
                                  "children": [],
                            },
                            {
                                  "id": 5,
                                  "title": "1.4",
                                  "children": [
                                        {
                                              "id": 6,
                                              "title": "1.4.1",
                                              "children": [],
                                        },
                                        {
                                              "id": 7,
                                              "title": "1.4.2",
                                              "children": [],
                                        }
                                  ],
                            },
                            {
                                  "id": 8,
                                  "title": "1.5",
                                  "children": [],
                            }
                      ]
                },
                {
                      "id": 2,
                      "title": "2",
                      "children": [],
                }
          ]
          //调用
          console.log(FlatArray(data))
    点赞 2 评论
  • weixin_42900858
    宣博文 2021-04-28 19:06
    let data = [{
        "id": 1,
        "title": "1",
        "children": [{
            "id": 2,
            "title": "1.1",
          },
          {
            "id": 3,
            "title": "1.2",
          },
          {
            "id": 4,
            "title": "1.3",
            "children": [],
          },
          {
            "id": 5,
            "title": "1.4",
            "children": [{
                "id": 6,
                "title": "1.4.1",
                "children": [],
              },
              {
                "id": 7,
                "title": "1.4.2",
              }
            ],
          },
          {
            "id": 8,
            "title": "1.5",
          }
        ]
      },
      {
        "id": 2,
        "title": "2",
        "children": [],
      }
    ]
    
    let newarr = [];
    
    function levelTreePro(arr, newarr) {
      arr.forEach(item => {
        if (item.children && item.children.length > 0) {
    
          newarr.push({
            id: item.id,
            title: item.title
          })
          levelTree(item.children, newarr)
        } else {
          newarr.push({
            id: item.id,
            title: item.title
          })
        }
      })
    };
    
    levelTreePro(data, newarr);
    console.log(newarr) //平级结果 
    点赞 2 评论
  • weixin_50524151
    Proud lion 2021-04-28 18:40
    let LevelArr= []
    function levelTree(arr, newarr) {
                    arr.forEach(item => {
                        if (item.children && item.children.length > 0) {
                            newarr.push(item)
                            levelTree(item.children, newarr)
                        } else {
                            newarr.push(item)
                        }
                    })
                }
      levelTree(data, LevelArr)
    点赞 2 评论
  • weixin_53110831
    第一凌 2021-04-29 09:57

    用递归

    点赞 评论

相关推荐