在下面的for循环中,当我第一次console.log searchTermsList[i]console.log('searchTermsList[i] is:' + searchTermsList[i]);)时,它可以正常工作并打印出相应的字符串。

但是,当我稍后在代码(console.log('the searchTermsList[i] Im about to use for the query is:' + searchTermsList[i]);)中再次执行此操作时,它会打印出未定义的内容。两个console.logs都在同一个循环中,那么第二个为什么不能找到该值?

for (var i = 0; (i < top3List.length) && (i < searchTermsList.length); i++){

        console.log('searchTermsList[i] is:' + searchTermsList[i]);
        console.log('top3List[i] is:' + top3List[i]);

        var MCI_Results = Parse.Object.extend("MCI_Results");
        var MCI_Results_Comparison_Query = new Parse.Query(MCI_Results);

        // Compare respective items' MCI_Results array to eBay results (top3List[i])
        MCI_Results_Comparison_Query.equalTo('parent', user);
        MCI_Results_Comparison_Query.contains('searchTerm', searchTermsList[i]);
        MCI_Results_Comparison_Query.containsAll('Results', top3List[i]);

        MCI_Results_Comparison_Query.find()
          .then(function(results) {

            // No new items, Results and top3List[i] are identical
            if (results.length > 0) {
              console.log('done updating channel');
            }

            // New items found, Results and top3List[i] don't match.
            else {
              console.log('no matching MCI_Results, lets push some new shit');

              // Find MCI_Results object for specific item
              var MCI_Results_Update_Query = new Parse.Query(MCI_Results);
              MCI_Results_Update_Query.equalTo('parent', user);
              console.log('the searchTermsList[i] Im about to use for the query is:' + searchTermsList[i]);
              MCI_Results_Update_Query.contains('searchTerm', searchTermsList[i]);

              // Update MCI_Results with new top3List eBay results
              MCI_Results_Update_Query.find()
                .then(function(results) {
                  console.log('totally just updated the MCI_Results, NBD');
                })
                .then(function() {
                  // Check for high priority MC items
                });
            }

          });
}

最佳答案

i是可变变量。 i++会在调用函数时将i更改为指向另一个索引。

您可能需要使用立即调用的匿名函数在新范围内创建一个新变量,而不要对其进行更改。

一个例子:

var makeBadAdders = function(n) {
    var adders = []
    for (var i = 0; i < n; i++)
        adders[i] = function(x) {
            // Closes over a mutable variable.
            // Function will use the most up-to-date value of i.
            return i + x;
        }
    return adders
}
var badAdders = makeBadAdders(3);
console.log(badAdders[1](1)); // 4
console.log(badAdders[2](1)); // Also 4

var makeAdders = function(n) {
    var adders = []
    for (var i = 0; i < n; i++)
        adders[i] = makeAdder(i);
    return adders
}
var makeAdder = function(i) {
    // Closes over an effectively immutable variable (i).
    return function(x) {
        return x + i;
    }
}

var goodAdders = makeAdders(3);
console.log(goodAdders[1](1)); // 2


请注意,您可以这样编写内联makeAdder

adders[i] = (function(i) {
    return x + i
})(i)


(这遮盖了外部可变的i。)

但是通常,最好避免使用可变变量,而使用类似forEach的变量。 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

10-08 04:38