我的递归函数在内部运行良好。但当被调用时,它不会。
function recursiveDataCreator(data, _level, _resultData){
let level = typeof _level == "undefined" ? 0 : _level;
let resultData = typeof _resultData == "undefined" ? [] : _resultData;
let setResultFunc = function(entry, upperKey, target){
if(upperKey == null || typeof upperKey == "undefined"){
target.push(entry);
}else{
target.forEach((it)=>{
if(it.key == upperKey){
if(typeof it.children == "undefined"){
it.children = [];
}
it.children.push(entry);
}
});
}
}
let lvData = data.filter((it)=>it.level==level);
let nextData = data.filter((it)=>it.level!=level);
lvData.forEach((it)=>{
let entry = {
key: it.key
, upperKey: it.upperKey
, title: it.title
, data: it.data
};
setResultFunc(entry, it.upperKey, resultData);
});
if(nextData.length == 0){
console.log("before return");
console.log(resultData);
return resultData;
}else{
recursiveDataCreator(nextData, (level+1), resultData);
}
}
这个案子很好。这是样本数据。
const initData1 = [
{ "level": 0, "key": "A100000", "upperKey": null, "title": "root_1", data: "dummy" },
{ "level": 0, "key": "A200000", "upperKey": null, "title": "root_2", data: "dummy" },
{ "level": 0, "key": "A300000", "upperKey": null, "title": "root_3", data: "dummy" },
{ "level": 0, "key": "A400000", "upperKey": null, "title": "root_4", data: "dummy" },
{ "level": 0, "key": "A500000", "upperKey": null, "title": "root_5", data: "dummy" }
];
let makedData1 = recursiveDataCreator(initData1);
console.log("after return");
console.log(makedData1);
这是样本数据结果。
before return
VM35:32 (5) [{…}, {…}, {…}, {…}, {…}]
VM35:48 after return
VM35:49 (5) [{…}, {…}, {…}, {…}, {…}]
另一个案例并不好。这是另一个样本数据。
const initData2 = [
{ "level": 0, "key": "A100000", "upperKey": null, "title": "root_1", data: "dummy" },
{ "level": 1, "key": "A101000", "upperKey": "A100000", "title": "root_1_1", data: "dummy" },
{ "level": 1, "key": "A102000", "upperKey": "A100000", "title": "root_1_2", data: "dummy" },
{ "level": 0, "key": "A200000", "upperKey": null, "title": "root_2", data: "dummy" },
{ "level": 1, "key": "A201000", "upperKey": "A200000", "title": "root_2_1", data: "dummy" },
{ "level": 2, "key": "A201010", "upperKey": "A201000", "title": "root_2_1_1", data: "dummy" },
{ "level": 1, "key": "A202000", "upperKey": "A200000", "title": "root_2_2", data: "dummy" }
];
let makedData2 = recursiveDataCreator(initData2);
console.log("after return");
console.log(makedData2);
这是另一个示例数据结果。
before return
VM35:32 (2) [{…}, {…}]
VM35:74 after return
VM35:75 undefined
为什么在返回之前存在的数据在返回之后变得未定义?
因为递归树的else下没有return
,所以返回值很可能只在树中最深的调用处停止。因此,当您尝试将值强制转换为变量时,该函数不会返回任何使其成为undefined
的内容。
let dataInRecursive;
function recursiveDataCreator(data, _level, _resultData){
let level = typeof _level == "undefined" ? 0 : _level;
let resultData = typeof _resultData == "undefined" ? [] : _resultData;
let setResultFunc = function(entry, upperKey, target){
if(upperKey == null || typeof upperKey == "undefined"){
target.push(entry);
}else{
target.forEach((it)=>{
if(it.key == upperKey){
if(typeof it.children == "undefined"){
it.children = [];
}
it.children.push(entry);
}
});
}
}
let lvData = data.filter((it)=>it.level==level);
let nextData = data.filter((it)=>it.level!=level);
lvData.forEach((it)=>{
let entry = {
key: it.key
, upperKey: it.upperKey
, title: it.title
, data: it.data
};
setResultFunc(entry, it.upperKey, resultData);
});
if(nextData.length == 0){
console.log("before return");
console.log(resultData);
dataInRecursive = resultData;
return resultData;
}else{
return recursiveDataCreator(nextData, (level+1), resultData);
}
}
const initData2 = [
{ "level": 0, "key": "A100000", "upperKey": null, "title": "root_1", data: "dummy" },
{ "level": 1, "key": "A101000", "upperKey": "A100000", "title": "root_1_1", data: "dummy" },
{ "level": 1, "key": "A102000", "upperKey": "A100000", "title": "root_1_2", data: "dummy" },
{ "level": 0, "key": "A200000", "upperKey": null, "title": "root_2", data: "dummy" },
{ "level": 1, "key": "A201000", "upperKey": "A200000", "title": "root_2_1", data: "dummy" },
{ "level": 2, "key": "A201010", "upperKey": "A201000", "title": "root_2_1_1", data: "dummy" },
{ "level": 1, "key": "A202000", "upperKey": "A200000", "title": "root_2_2", data: "dummy" }
];
let makedData2 = recursiveDataCreator(initData2);
console.log("after return");
console.log(makedData2);
// Check if the returned data same or not.
console.log(makedData2 === dataInRecursive);