# 数据转换

数组转树形结构

[
  { key: "key4", parent: "key3" },
  { key: "key5", parent: "key2" },
  { key: "key1", parent: "" },
  { key: "key2", parent: "" },
  { key: "key3", parent: "key1" },
];
// 转变为
[
  {
    key: "key1",
    parent: "key0",
    children: [
      {
        key: "key3",
        parent: "key1",
        children: [
          {
            key: "key4",
            parent: "key3",
          },
        ],
      },
    ],
  },
  {
    key: "key2",
    parent: "key0",
    children: [{ key: "key5", parent: "key2" }],
  },
];

// 好的实现
function toTree(data) {
  let result = [];
  if (!Array.isArray(data)) {
    return result;
  }
  let map = {};
  data.forEach((item) => {
    map[item.key] = item;
  });
  data.forEach((item) => {
    let parent = map[item.parent];
    if (parent) {
      (parent.children || (parent.children = [])).push(item);
    } else {
      result.push(item);
    }
  });
  return result;
}

// 通过递归,需要保证原始数据层级关系顺序才行
function arrToTree(list, pid = "") {
  let children = [];
  list.forEach((element) => {
    if (!element.parent || element.parent === pid) {
      // 先把一级数组添加到children里
      children.push(element);
      // 然后立刻递归找他的儿子
      element.children = arrToTree(list, element.key);
    }
  });
  return children;
}

// 利用对象是引用数据特性
function convert(list) {
  const obj = {};
  const knowedKeys = list.map((item) => item.key);
  console.log(knowedKeys);
  list.forEach((item) => {
    obj[item.key] = item;
  });
  list.forEach((item) => {
    if (item.parentKey && obj[item.parentKey]) {
      obj[item.parentKey].children = obj[item.parentKey].children || [];
      obj[item.parentKey].children.push(item);
    }
  });
  // 将非第一级的删除
  list.forEach((item) => {
    if (item.parentKey && knowedKeys.includes(item.parentKey)) {
      delete obj[item.key];
    }
  });
  let res = Object.keys(obj).map((key) => {
    return obj[key];
  });
  return res;
}

function convert(list) {
  let knowedKeys = [...new Set(list.map((i) => i.key))];
  let setMap = {};
  let result = [];
  function find(result, key) {
    for (let i = 0; i < result.length; i++) {
      let item = result[i];
      if (item.key === key) {
        return item;
      }
      if (item.children && item.children.length) {
        let has = find(item.children, key);
        if (has) {
          return has;
        }
      }
    }
    return null;
  }
  function set(list) {
    for (let i = 0; i < list.length; i++) {
      const item = list[i];
      if (!knowedKeys.includes(item.parentKey)) {
        result.push(item);
        setMap[item.key] = 1;
        list.splice(i, 1);
        i--;
        continue;
      }
      if (knowedKeys.includes(item.parentKey) && setMap[item.parentKey]) {
        let parent = find(result, item.parentKey);
        if (parent) {
          parent.children = parent.children || [];
          parent.children.push(item);
          setMap[item.key] = 1;
          list.splice(i, 1);
          i--;
        }
      }
    }
    if (list.length > 0) {
      set(list);
    }
  }
  set(list);
  return result;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140