我有些麻烦,为什么我的递归函数不断返回假。
const location = {
name: "917",
parentLocationCluster: {
name: "Key Zones"
ParentLocationCluster: {
name: "Bla"
}
}
}
const test = CheckIfInKeyZone(location)
const CheckIfInKeyZone = (parentLocationCluster) => {
if(parentLocationCluster.name === 'Key Zones') {
return true;
}
else if(parentLocationCluster.parentLocationCluster) {
const { parentLocationCluster: location } = parentLocationCluster;
CheckIfInKeyZone(location);
}
return false;
};
parentLocationCluster.name === 'Key Zones'
被命中,我希望返回值是true,但是不是。救命?
最佳答案
小错误应该是:
返回CheckIfInKeyZone(location);