在Typescript中使用循环推送到数组 [英] Push onto array with loop in Typescript

查看:94
本文介绍了在Typescript中使用循环推送到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,这是我之前的问题的答案:遍历2d Typescript数组,并从索引处的值创建新数组(如果可以)。我以为我会转贴,因为否则原件。帖子会变得太复杂。

I have the following code, which is the answer to my earlier question: Looping through 2d Typescript array and making new array from value at index if only it worked. I thought I would repost because otherwise the orig. post would get too complicated.

我不明白为什么我似乎无法推送到数组或无法在循环外访问数组?

I don't understand why I can't seem to push to an array, or access the array outside the loop?

  this.days_in_month = [
    [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], 
  ];

  this.month_weekdays = [];
  this.days_in_month.forEach(function each(item) {
      // console.log(item);
      item.forEach(function each(value){
        // console.log(value);

        let thing: number = value;
          for(var i = 0; i < thing; i++) {
            let weekdays: string[] = [ 'M','T','W','T','F','S','S' ];
            let wkday: string = (weekdays[(i%7)]);
            this.month_weekdays.push(wkday);
          };
      });
  });

我认为 this.month_weekdays.push(wkday);应该将字符串wkday推送到month_weekdays数组中,以便对于阵列days_in_month中的每个值都应遍历工作日,并将索引i%7处的字符串分配给month_weekdays的索引i

I think "this.month_weekdays.push(wkday);" should push the string wkday to the array month_weekdays, so that for every value in the array days_in_month it should loop through weekdays and assign the string at index i%7 to index i of month_weekdays

因此,days_in_month month_weekdays中的第一个值应如下所示:

So for the first value in days_in_month month_weekdays should look like:

MTWTFSSMTWTFSSMTWTFSSMTWTFSSMTW

MTWTFSSMTWTFSSMTWTFSSMTWTFSSMTW

(并且因为12月有31天,这也应该是month_weekdays的最终值)

(And because December has 31 days this should also be the final value of month_weekdays)

推荐答案

我认为问题是因为您使用 function(){} 语法,而不绑定 this 。因此, this.month_weekdays 不能正确获取正确的 month_weekdays 。有两种选择:

I think the problem occurs because you use function() {} syntax without binding the this. Therefore this.month_weekdays doesn't exactly got the correct month_weekdays. There are two options:


  1. 将函数更改为箭头语法,或者

  2. 绑定 this 在您的 function(){}

  1. Changing your function to arrow syntax, or
  2. Bind this in your function() {}

对于第一个选项,您的代码将如下所示:

For option number one, your code will look like this:

this.days_in_month.forEach((item) => {
  // console.log(item);
  item.forEach((value) => {
    // console.log(value);

    let thing: number = value;
      for(var i = 0; i < thing; i++) {
        let weekdays: string[] = [ 'M','T','W','T','F','S','S' ];
        let wkday: string = (weekdays[(i%7)]);
        this.month_weekdays.push(wkday);
      };
  });

});

您应该可以在循环外访问 month_weekdays 。发生的是 function(){} 语法具有自己的 this ,因此您必须绑定 this 从外部作用域,以便 function(){} 引用正确的 this 。另一方面,箭头语法比 function(){} 短,并且不绑定自己的 this 参数 super new.target ;因此不需要的绑定。

You should be able to access your month_weekdays outside the loop. What happen is that function() {} syntax have its own this, therefore you have to bind the this from the outer scope so that the function(){} refers the correct this. On the other hand, arrow syntax is a shorter syntax than the function(){} and does not bind its own this, arguments, super, or new.target; thus no binding of this is required.

对于第二个选项,您的代码会看起来像这样:

For option number two, your code will look like this:

this.days_in_month.forEach(function(item) {
  // console.log(item);
  item.forEach(function(value) {
    // console.log(value);

    let thing: number = value;
      for(var i = 0; i < thing; i++) {
        let weekdays: string[] = [ 'M','T','W','T','F','S','S' ];
        let wkday: string = (weekdays[(i%7)]);
        this.month_weekdays.push(wkday);
      };
  }.bind(this));
}.bind(this));

这篇关于在Typescript中使用循环推送到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆