原方法:
/**
* 构造树型结构数据
* @param {*} data 数据源
* @param {*} id id字段 默认 'id'
* @param {*} parentId 父节点字段 默认 'parentId'
* @param {*} children 孩子节点字段 默认 'children'
*/
function handleTree(data, id, parentId, children) {
let config = {
id: id || "id",
parentId: parentId || "parentId",
childrenList: children || "children",
};
let childrenListMap = {};
let nodeIds = {};
let tree = [];
for (let d of data) {
let parentId = d[config.parentId];
if (childrenListMap[parentId] == null) {
childrenListMap[parentId] = [];
}
nodeIds[d[config.id]] = d;
childrenListMap[parentId].push(d);
}
for (let d of data) {
let parentId = d[config.parentId];
if (nodeIds[parentId] == null) {
tree.push(d);
}
}
for (let t of tree) {
adaptToChildrenList(t);
}
function adaptToChildrenList(o) {
if (childrenListMap[o[config.id]] !== null) {
o[config.childrenList] = childrenListMap[o[config.id]];
}
if (o[config.childrenList]) {
for (let c of o[config.childrenList]) {
adaptToChildrenList(c);
}
}
}
return tree;
}
新增参数,以指定字段为依据扩展最底层叶子,在第一次data遍历中间加入下面的代码:
// 扩展:新增第二字段拼接为最后一层children
if (config.hasSecond) {
let sChildren = undefined;
if (d[config.secondChildren] !== undefined && d[config.secondChildren].length > 0) {
sChildren = [];
let ogcs = d[config.secondChildren];
ogcs.forEach((ogc) => {
ogc[config.parentId] = d[config.id];
ogc[config.label] = ogc[config.secondLabel];
sChildren.push(ogc);
});
}
d[config.childrenList] = sChildren;
}
运行后不起作用,debug发现中途新加的children的确挂上了,但是在遍历迭代tree之后又变回了children。
最后发现是最后遍历tree时,为每个一级node挂的children数据出自childrenListMap,以id为索引,而扩展加的children,没有挂到childrenListMap上去,于是在索引过程中childrenListMap[原本没有children的数据的id]无数据,就被重新赋值回undefined了。(写得有点绕)(实际也确实绕)
解决问题后的完整代码:
/**
* 构造树型结构数据
* @param {*} data 数据源
* @param {*} id id字段 默认 'id'
* @param {*} parentId 父节点字段 默认 'parentId'
* @param {*} children 孩子节点字段 默认 'children'
* @param {*} secondChildren 最后拼接的节点判断字段,可为空 默认 'OGCs'
* @param {*} label 要给第二子节点赋值能显示的标签,需要定义主树用的标签字段 默认 'dataName'
* @param {*} secondLabel 要给第二子节点赋值能显示的标签,需要定义映射到主树标签字段的字段 默认 'ogc_name'
*/
function handleTree(data, id, parentId, children, secondChildren, label, secondLabel) {
let config = {
id: id || "id",
parentId: parentId || "parentId",
childrenList: children || "children",
hasSecond: secondChildren !== undefined, // 标记是否有第二子节点字段
secondChildren: secondChildren || "OGCs",
label: label || "dataName",
secondLabel: secondLabel || "ogc_name",
};
let childrenListMap = {};
let nodeIds = {};
let tree = [];
for (let d of data) {
let parentId = d[config.parentId];
if (childrenListMap[parentId] == null) {
childrenListMap[parentId] = [];
}
// 扩展:新增第二字段拼接为最后一层children
if (config.hasSecond) {
let sChildren = undefined;
if (d[config.secondChildren] !== undefined && d[config.secondChildren].length > 0) {
sChildren = [];
let ogcs = d[config.secondChildren];
ogcs.forEach((ogc) => {
ogc[config.parentId] = d[config.id];
ogc[config.label] = ogc[config.secondLabel];
sChildren.push(ogc);
});
childrenListMap[d[config.id]] = sChildren; // 解决children加上最后又undefined的bug
}
d[config.childrenList] = sChildren;
}
nodeIds[d[config.id]] = d;
childrenListMap[parentId].push(d);
}
for (let d of data) {
let parentId = d[config.parentId];
if (nodeIds[parentId] == null) {
tree.push(d);
}
}
for (let t of tree) {
adaptToChildrenList(t);
}
function adaptToChildrenList(o) {
if (childrenListMap[o[config.id]] !== null) {
o[config.childrenList] = childrenListMap[o[config.id]];
}
if (o[config.childrenList]) {
for (let c of o[config.childrenList]) {
adaptToChildrenList(c);
}
}
}
return tree;
}
一点经验积累:
- object赋值失败,可能是需要用set强制修改(本例不是)
- 也可能本来赋值成功,但后面又被改掉了,这种情况用console.log会显示最终值,不可靠,需要打断点检查
- 除此之外就要考虑更弱的原因,比如明显的编码失误