[英]Why is this javascript function returning undefined ?
let getLowerUpperBoundFromValue=(bound , idToValue)=>{
// Returns the value of the variable previously generated if "value" is a variable name or return "value" if its a number .
// bound : can be a direct integer or a variable name.
// idToValue : contains the id to value mapping which must contain a variable whose name must be equal to 'bound' parameter if its a variable name .
if(isNaN(Number(bound)))
{
Object.entries(idToVarStatesGlobal).forEach(idStatePair=>{
let id= idStatePair[0] , varState = idStatePair[1] ;
if(varState.name===bound){
console.log("check now Returning idTovalue[id]" , idToValue , id , idToValue[id] , Number(idToValue[id]));
return Number(idToValue[id]) ;
}
})
}
else return Number(bound);
}
当我做这样的控制台日志时:
console.log('check now: ' , getLowerUpperBoundFromValue(varState.lowerbound , idToValue)) ;
我得到这样的日志输出:
check now Returning idTovalue[id] {PSfCL5hBm: 69} PSfCL5hBm 69 69
inputGeneration.js:99 check now: undefined
为什么即使Number(idTovalue[id])
值返回到正常值69,该函数也仍返回未定义状态?
什么也没有返回,因为forEach回调是一个单独的方法。 我删除了对.forEach
的调用,并用for of
循环替换了它,该循环维护了return
范围
let getLowerUpperBoundFromValue=(bound , idToValue)=>{
// Returns the value of the variable previously generated if "value" is a variable name or return "value" if its a number .
// bound : can be a direct integer or a variable name.
// idToValue : contains the id to value mapping which must contain a variable whose name must be equal to 'bound' parameter if its a variable name .
if(isNaN(Number(bound)))
{
for (let idStatePair of Object.entries(idToVarStatesGlobal)) {
let id= idStatePair[0] , varState = idStatePair[1] ;
if(varState.name===bound){
console.log("check now Returning idTovalue[id]" , idToValue , id , idToValue[id] , Number(idToValue[id]));
return Number(idToValue[id]) ;
}
}
}
else return Number(bound);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.