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);
};
});
});
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
So for the first value in days_in_month month_weekdays should look like:
MTWTFSSMTWTFSSMTWTFSSMTWTFSSMTW
(And because December has 31 days this should also be the final value of month_weekdays)