javaScript 递归生成树形tree结构and树形tree结构还原数组

数据结构 

树数据
[{
	"id": 1,
	"name": "1级1",
	"pid": 0,
	"children": [{
		"id": 3,
		"name": "1级1-1",
		"pid": 1
	}]
}, {
	"id": 2,
	"name": "2级1",
	"pid": 0,
	"children": [{
		"id": 5,
		"name": "2级1-1",
		"pid": 2
	}]
}, {
	"id": 6,
	"name": "3级1",
	"pid": 0,
	"children": [{
		"id": 8,
		"name": "1级1-1-1-1",
		"pid": 6
	}]
}]

数组数据
[
  {
    "id": 1,
    "name": "1级1",
    "pid": 0
  },
  {
    "id": 3,
    "name": "1级1-1",
    "pid": 1
  },
  {
    "id": 2,
    "name": "2级1",
    "pid": 0
  },
  {
    "id": 5,
    "name": "2级1-1",
    "pid": 2
  },
  {
    "id": 6,
    "name": "3级1",
    "pid": 0
  },
  {
    "id": 8,
    "name": "1级1-1-1-1",
    "pid": 6
  }
]

 源码:


你可能感兴趣的:(#,vue,前段,javascript,前端,开发语言)