【发布时间】:2019-04-30 18:24:25
【问题描述】:
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]) evalues 为正常值 69 ,函数仍返回 undefined ?
【问题讨论】:
-
因为
return语句在forEach回调内部,而不是在getLowerUpperBoundFromValue内部。从“内部”函数返回不会神奇地从外部函数返回。简单示例:function outer() { function inner() { return 'inner'; }; inner(); return 'outer'; }; outer();返回"outer"。 -
@Solo:
forEach不返回任何东西,所以这也无济于事。 -
forEach 用于在数组中查找索引/条目是错误的。你应该使用
find()
标签: javascript reactjs function ecmascript-6