Javascript 循环函数的返回值
此Meteor服务器递归方法Javascript 循环函数的返回值,javascript,meteor,Javascript,Meteor,此Meteor服务器递归方法commonHint将result未定义返回控制台,即使finalRes有值。 关于如何将finalRes返回给调用者有什么建议吗?thx //call the recursive method let result = this.commonHint(myCollection.findOne({age: 44}), shortMatches); console.log('got most common hint: ' + result); /
commonHint
将result
未定义返回控制台,即使finalRes
有值。关于如何将
finalRes
返回给调用者有什么建议吗?thx
//call the recursive method
let result = this.commonHint(myCollection.findOne({age: 44}), shortMatches);
console.log('got most common hint: ' + result); // <=== undefined ====
'commonHint': function (doc, shortMatches, hinters, results = []) {
// first call only first 2 args are defined,
if (!hinters) {
hinters = [...lib.getCombinations(['arg1', 'arg2', 'arg3'], 2, 3)];
this.commonHint(doc, shortMatches, hinters, results); // hinters is an array of length 3 with 2 elements each
return;
}
// get an element from hinters, use its 2 hinters and remove that element from the hinters
let hintersToUse = hinters.pop();
let hinter1 = this.cleanMatchItem(hintersToUse[0]);
let hinter2 = this.cleanMatchItem(hintersToUse[1]);
let intersect = _.intersection(hinter1, hinter2);
// which item of the shortMatches best matches with the intersect
let tempCol = new Meteor.Collection();
for (let i = 0; i < shortMatches.length; i++) { tempCol.insert({match: shortMatches[i]}); }
results.push(mostSimilarString(tempCol.find({}), 'match', intersect.join(' ')));
if (hinters.length > 0) {
this.commonHint(doc, shortMatches, hinters, results);
} else {
let finalRes = lib.mostCommon(results);
console.log(finalRes); //<==== has a value
return finalRes; //<==== so return it to caller
}
},
//调用递归方法
让result=this.commonHint(myCollection.findOne({age:44}),shortMatches);
console.log('得到最常见的提示:'+结果);//0) {
这个.commonHint(文档、短匹配、提示、结果);
}否则{
让finalRes=lib.mostCommon(结果);
console.log(finalRes);//您调用的任何地方commonHint
您都需要返回调用的值
...
if (!hinters) {
hinters = [...lib.getCombinations(['arg1', 'arg2', 'arg3'], 2, 3)];
return this.commonHint(doc, shortMatches, hinters, results); // hinters is an array of length 3 with 2 elements each
}
...
if (hinters.length > 0) {
return this.commonHint(doc, shortMatches, hinters, results);
您调用的任何位置commonHint
都需要返回调用的值
...
if (!hinters) {
hinters = [...lib.getCombinations(['arg1', 'arg2', 'arg3'], 2, 3)];
return this.commonHint(doc, shortMatches, hinters, results); // hinters is an array of length 3 with 2 elements each
}
...
if (hinters.length > 0) {
return this.commonHint(doc, shortMatches, hinters, results);
返回结果的递归函数中的每个路径都必须返回结果。在您的递归函数中,有不返回结果的路径:当未提供hinters
时,以及当hinters.length>0
为真时
您应该返回递归调用的结果:
if (!hinters) {
hinters = [...lib.getCombinations(['arg1', 'arg2', 'arg3'], 2, 3)];
return this.commonHint(doc, shortMatches, hinters, results); // hinters is an array of length 3 with 2 elements each
// ^^^^^^
}
// ...
if (hinters.length > 0) {
return this.commonHint(doc, shortMatches, hinters, results);
// ^^^^^^
} else {
let finalRes = lib.mostCommon(results);
console.log(finalRes); //<==== has a value
return finalRes; //<==== so return it to caller
}
if(!提示){
hinters=[…lib.getCombinations(['arg1','arg2','arg3'],2,3)];
返回此.commonHint(doc、shortMatches、hinters、results);//hinters是一个长度为3的数组,每个数组有2个元素
// ^^^^^^
}
// ...
如果(hinters.length>0){
返回此.commonHint(文档、短匹配、提示、结果);
// ^^^^^^
}否则{
让finalRes=lib.mostCommon(结果);
console.log(finalRes);//递归函数中返回结果的每个路径都必须返回结果。在您的函数中,有不返回结果的路径:当未提供hinters
时,以及当hinters.length>0
为true时
您应该返回递归调用的结果:
if (!hinters) {
hinters = [...lib.getCombinations(['arg1', 'arg2', 'arg3'], 2, 3)];
return this.commonHint(doc, shortMatches, hinters, results); // hinters is an array of length 3 with 2 elements each
// ^^^^^^
}
// ...
if (hinters.length > 0) {
return this.commonHint(doc, shortMatches, hinters, results);
// ^^^^^^
} else {
let finalRes = lib.mostCommon(results);
console.log(finalRes); //<==== has a value
return finalRes; //<==== so return it to caller
}
if(!提示){
hinters=[…lib.getCombinations(['arg1','arg2','arg3'],2,3)];
返回此.commonHint(doc、shortMatches、hinters、results);//hinters是一个长度为3的数组,每个数组有2个元素
// ^^^^^^
}
// ...
如果(hinters.length>0){
返回此.commonHint(文档、短匹配、提示、结果);
// ^^^^^^
}否则{
让finalRes=lib.mostCommon(结果);
控制台日志(最终)//