代码
let sourceArr =
[
{
id: "1",
data: { menuName: "one" },
childTreeNode: [
{ data: {menuName: "one-1"}, childTreeNode: [] },
{ data: {menuName: "one-2"}, childTreeNode: [] },
{ data: {menuName: "one-3"}, childTreeNode: [] }
]
},
{
id: "2",
data: { menuName: "two" },
childTreeNode: [
{
data: {menuName: "two-1"},
childTreeNode: [
{ data: {menuName: "two-1-1"}, childTreeNode: [] }
]
}
]
},
{
id: "3",
data: { menuName: "three" },
childTreeNode: []
}
]
let resultArr = [];
function DispatchValue (value, index, id, parentIndex) {
let _value = value
// 最顶级有子节点
if(value.childTreeNode.length != 0 && value.id) {
// 首次调用无id 设置OriginId值,小环境内不再变化
if (!id) {
DispatchValue.prototype.level.originID = value.id;
}
resultArr.push({id: value.id, menuName: value.data.menuName, deep: DispatchValue.prototype.level.deep})
DispatchValue.prototype.level.deep ++
InfientLoop(value.childTreeNode, value.id, index)
DispatchValue.prototype.level.deep --
} else
// 次级子节点
if (value.childTreeNode.length != 0 && !value.id) {
resultArr.push({originID: DispatchValue.prototype.level.originID, parentIndex: parentIndex, index: index, menuName: value.data.menuName, deep: DispatchValue.prototype.level.deep})
DispatchValue.prototype.level.deep ++
InfientLoop(value.childTreeNode, value.id, index)
DispatchValue.prototype.level.deep --
} else
// 末级节点
if(value.childTreeNode.length == 0 && !value.id){
resultArr.push({originID: DispatchValue.prototype.level.originID, parentIndex: parentIndex, index: index, menuName: value.data.menuName, deep: DispatchValue.prototype.level.deep})
}
// 最顶级节点,没有子节点
else {
resultArr.push({id: value.id, menuName: value.data.menuName, deep: DispatchValue.prototype.level.deep})
}
}
DispatchValue.prototype.level = {
deep: 0,
originID: ''
}
DispatchValue.prototype.constructor = DispatchValue
function InfientLoop (Arr, id, parentIndex) {
Arr.forEach(function (v, i) {
DispatchValue(v, i, id, parentIndex)
});
}
InfientLoop(sourceArr)
console.log(resultArr)
网站导航
网站导航