JavaScript 数据树索引设置函数 - ES6 优化代码
function setIndex(data) {
let queue = [...data];
let loop = 0;
while (queue.length > 0) {
loop++;
for (const [i, child] of [...queue].entries()) {
queue.shift();
if (loop == 1) {
child.customIndex = ${i + 1};
child.currentIndex = i;
child.path = i;
}
if (child.children && child.children.length > 0) {
child.dataType = 1;
for (const [ci, cChild] of child.children.entries()) {
cChild.currentIndex = ci;
cChild.customIndex = ${child.customIndex}.${ci + 1};
cChild.path = ${child.path}.children.${ci};
}
queue.push(...child.children);
} else {
child.dataType = 2;
}
}
}
}
原文地址: https://www.cveoy.top/t/topic/oPdN 著作权归作者所有。请勿转载和采集!