访问forEach回调中的变量

时间:2015-07-15 20:39:07

标签: javascript arrays

我想访问一个变量,其目的是汇总到目前为止提供给forEach的回调内部的记录。像这样:

var dataContext = {
      message: "To set up your profile follow the link",
      url: "{{pathFor 'welcome'}}",
      hyperlinkText: "Get Started",
      title: "Welcome to site!"
    };


    var html = Blaze.toHTMLWithData(Template.emailTemplate, dataContext);
    var from = "myemail@mysite.com";
    var to = currentUser.emails[0].address;
    var subject = "Welcome to site";

    Meteor.call("sendEmail", to, from, subject, html);

为什么不能这样做?

1 个答案:

答案 0 :(得分:1)

您无需使用this来引用数组aggregate_val。试试这段代码:

var myfn = function() {
  var aggregate_val = [];
  some_array.forEach(function(e) {
    aggregate_val.push(e.some_property);
  });
  console.log(some_array) // I added this so you can see the value of some_array
}