为什么这个函数返回未定义?它在函数内部有一个值,但是一旦我尝试将它分配给一个新变量,它就会返回为未定义。
function getLookupDefault(lookupModel) {
Object.keys(lookupModel.LookupValues).forEach(function (key) {
if (lookupModel.LookupValues[key].IsDefault == true) {
test = lookupModel.LookupValues[key].Name;
console.log("test: " + test);
return test;
}
})
};
var tst = getLookupDefault(model.LookupValuesDelimiter);
console.log("tst: " + tst);
编辑: 谢谢。来自 c#,这对我来说并不明显。我已经编辑了代码,它可以正常工作。
function getLookupDefault(lookupModel) {
for (var key in Object.keys(lookupModel.LookupValues)) {
if (lookupModel.LookupValues[key].IsDefault == true) {
test = lookupModel.LookupValues[key].Name;
console.log("test: " + test);
return test;
}
}
}
最佳答案
其中的 return 语句不会向外部函数返回值,它只会向 forEach 调用的内部函数返回一个值。
关于javascript - 为什么这个 javascript 函数返回未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49165873/