Browse Source

js 基础函数记录
1. 数组转树
2. 深拷贝

kindring 2 years ago
parent
commit
f04a163b20
3 changed files with 108 additions and 0 deletions
  1. 67 0
      js/arrayToTree.js
  2. 19 0
      js/deepClone.js
  3. 22 0
      方向/中控系统.md

+ 67 - 0
js/arrayToTree.js

@@ -0,0 +1,67 @@
+let testData = [
+    {
+      groupId: 1,
+      name: 1,
+      parentId: ''
+    },
+    {
+      groupId: 2,
+      name: 2,
+      parentId: ''
+    },{
+      groupId: 3,
+      name: 3,
+      parentId: ''
+    },{
+      groupId: 4,
+      name: 1.4,
+      parentId: 1
+    },
+    {
+      groupId: 5,
+      name: 1.5,
+      parentId: 1
+    },
+    {
+      groupId: 6,
+      name: 1.6,
+      parentId: 1
+    },
+    {
+      groupId: 7,
+      name: 6.7,
+      parentId: 6
+    },
+    {
+      groupId: 8,
+      name: 6.8,
+      parentId: 6
+    },
+    {
+      groupId: 9,
+      name: 2.9,
+      parentId: 2
+    },
+    {
+      groupId: 10,
+      name: 2.10,
+      parentId: 2
+    }
+  ];
+function arrayToTree(array,oneLevelId='',fieldKey,parentKey){
+    // 数组转tree
+    function loop(_arr,parentId){
+      return _arr.reduce((acc,cur,ind,arr)=>{
+        if(cur[parentKey] === parentId){
+          acc.push(cur);
+          cur.children = loop(arr,cur['fieldKey']);
+        }
+        return acc;
+      },[]);
+    }
+    return loop(array,oneLevelId);
+  }
+
+function test(){
+    arrayToTree(testData);// array [{…}, {…}, {…}]
+}

+ 19 - 0
js/deepClone.js

@@ -0,0 +1,19 @@
+function deepClone(obj, hash = new WeakMap()) {
+    if (obj === null) return obj; // 如果是null或者undefined我就不进行拷贝操作
+    if (obj instanceof Date) return new Date(obj);
+    if (obj instanceof RegExp) return new RegExp(obj);
+    // 可能是对象或者普通的值  如果是函数的话是不需要深拷贝
+    if (typeof obj !== "object") return obj;
+    // 是对象的话就要进行深拷贝
+    if (hash.get(obj)) return hash.get(obj);
+    let cloneObj = new obj.constructor();
+    // 找到的是所属类原型上的constructor,而原型上的 constructor指向的是当前类本身
+    hash.set(obj, cloneObj);
+    for (let key in obj) {
+      if (obj.hasOwnProperty(key)) {
+        // 实现一个递归拷贝
+        cloneObj[key] = deepClone(obj[key], hash);
+      }
+    }
+    return cloneObj;
+  }

+ 22 - 0
方向/中控系统.md

@@ -0,0 +1,22 @@
+# 中控系统结构
+## 中控服务项目结构
+### 结构图
+```mermaid
+graph TB
+  S(server) --scoket--> A[主机A 客户端]--socket--> S
+ 
+  S --scoket--> B[主机B 客户端] --socket--> S
+  S --scoket--> C[主机C 客户端] --socket--> S
+  S --socket--> NET[其他网络设备] --socket--> S
+  NET --> D1[tcp设备]
+  NET --> D2[udp设备]
+  NET --> TY[投影设备]
+  D1 --> g1[可计算已知设备]
+  D2 --> g1
+  D1 --> g2[已知指令设备]
+  D2 --> g2
+  control[pad等] --网络连接--> S
+```
+## 实体数据结构
+
+