<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let oldTreeList = [
{ id: 1, lv: 1, tecName: '基础科学-1', belong: 'node1' },
{ id: 2, lv: 2, tecName: '基础科学-2', belong: 'node1' },
{ id: 3, lv: 3, tecName: '基础科学-3', belong: 'node1' },
]
let newTreeList = [
{ id: 1, lv: 0, tecName: '基础科学-1', belong: 'node1' },
{ id: 2, lv: 1, tecName: '基础科学-2 修改', belong: 'node1' },
{ id: 3, lv: 2, tecName: '基础科学-3', belong: 'node1' },
{ id: 4, lv: 2, tecName: '基础科学-3', belong: 'node1' },
{ id: 5, lv: 0, tecName: '基础科学-1', belong: 'node2' },
{ id: 6, lv: 1, tecName: '基础科学-2 修改', belong: 'node2' },
{ id: 7, lv: 2, tecName: '基础科学-3', belong: 'node2' },
{ id: 8, lv: 2, tecName: '基础科学-3', belong: 'node2' },
]
function compareTreeLists(oldList, newList) {
const result = [];
function findNode(id, list) {
return list.find(node => node.id === id);
}
function compareNodes(oldNode, newNode) {
if (!newNode) {
return { ...oldNode, type: 'delete' };
} else {
const children = [];
const oldChildren = oldNode ? oldNode.children : [];
const newChildren = newNode.children || [];
for (let i = 0; i < newChildren.length; i++) {
const oldChild = findNode(newChildren[i].id, oldChildren);
children.push(compareNodes(oldChild, newChildren[i]));
}
if (!oldNode || !Object.entries(oldNode).every(([key, value]) => newNode[key] === value)) {
return { ...newNode, children, type: oldNode ? 'update' : 'add' };
}
return { ...newNode, children };
}
}
for (let i = 0; i < oldList.length; i++) {
const newNode = findNode(oldList[i].id, newList);
result.push(compareNodes(oldList[i], newNode));
}
return result;
}
const comparisonResult = compareTreeLists(oldTreeList, newTreeList);
console.log(comparisonResult);
function compareTreeLists1(oldList, newList) {
const result = [];
function findNode(id, list) {
return list.find(node => node.id === id);
}
function compareNodes(oldNode, newNode) {
if (!newNode) {
return { ...oldNode, type: 'delete' };
} else {
const children = [];
const oldChildren = oldNode ? oldNode.children : [];
const newChildren = newNode.children || [];
for (let i = 0; i < newChildren.length; i++) {
const oldChild = findNode(newChildren[i].id, oldChildren);
children.push(compareNodes(oldChild, newChildren[i]));
}
if (!oldNode || !Object.entries(oldNode).every(([key, value]) => newNode[key] === value)) {
return { ...newNode, children, type: oldNode ? 'update' : 'add' };
}
return { ...newNode, children };
}
}
for (let i = 0; i < oldList.length; i++) {
const newNode = findNode(oldList[i].id, newList);
result.push(compareNodes(oldList[i], newNode));
}
for (let i = 0; i < newList.length; i++) {
if (!findNode(newList[i].id, oldList)) {
result.push({ ...newList[i], type: 'add' });
}
}
return result;
}
const comparisonResult1 = compareTreeLists1(oldTreeList, newTreeList);
console.log(comparisonResult1);
function convertTreeData(list) {
console.log(list)
const deleteIds = []
list.forEach((i) => {
i.children = []
list.forEach((item) => {
if (item.lv === i.id && item.lv !== 0) {
i.children.push(item)
deleteIds.push(item.id)
}
})
if (i.children.length === 0) {
delete i.children
}
})
const result = list.filter(i => !deleteIds.includes(i.id))
return result
}
console.log(convertTreeData(newTreeList))
function arrayGroup(arr) {
console.log(arr)
const m = new Map();
arr.map(item => {
m.set(item.belong, [...(m.get(item.belong) || ''), item])
})
console.log(m.entries())
return Object.fromEntries(m.entries());
}
let batchSetForm = arrayGroup(newTreeList)
function batchSet(batchSetForm) {
let changeTreeList = []
console.log(batchSetForm)
for (const key in batchSetForm) {
if (Object.hasOwnProperty.call(batchSetForm, key)) {
console.log(batchSetForm, key)
const element = batchSetForm[key];
if (key == 'node2') {
console.log(convertTreeData(element), element)
}
}
}
console.log(changeTreeList)
return changeTreeList
}
console.log(convertToTree(newTreeList))
</script>
</body>
</html>