Angular typescript以循环方式将元素推入数组

Angular typescript以循环方式将元素推入数组,angular,typescript,Angular,Typescript,我有一个数组: public roundRobinMonths: any=['Jan', 'Feb', 'Mar', 'Apr', 'Mai', 'Jun', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; 另一个空数组:public-userTimeline:any=[] 我的用户将从日期选择器中选择一个月,如果他选择说9月,那么我想在我的userTimeline数组中添加从10月开始到9月的月份,即10月、11月、…、Apl、Mai、…、8月、9月

我有一个数组:

public roundRobinMonths: any=['Jan', 'Feb', 'Mar', 'Apr', 'Mai', 'Jun', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
另一个空数组:
public-userTimeline:any=[]

我的用户将从日期选择器中选择一个月,如果他选择说9月,那么我想在我的userTimeline数组中添加从10月开始到9月的月份,即10月、11月、…、Apl、Mai、…、8月、9月

我在我的组件索引中收到用户选择的一个月,即0、1、11

我尝试运行如下所示的for循环:

public pickDate($event: MouseEvent) {
    this.datePicker.selectDate(this.selectedWeddingDate, {openFrom: $event}).subscribe( (selectedDate: Date) => {

        this.selecteDate = selectedDate ? moment(selectedDate) : null;

        // logic for creating personalized timeline
        switch (this.selectedDate.month()) {
        case 8:
            for(let month of this.roundRobinMonths){
                if(this.roundRobinMonths.indexOf(month)===8){
                    this.userTimeline.push(month)
                    console.log(this.userTimeline)
                }
            }
            break;

        default:
            // code...
            break;
    }

    });
}
这只增加了一个月。正确的逻辑是什么

// store the selected month as an index
const selectedMonth = this.selectedDate.month(); 

// get all months after the selected one
// (we get all between the selected one and the end)
const monthsAfterSelected
    = this.roundRobinMonths.slice(selectedMonth + 1, this.roundRobinMonths.length);

// get all months before the selected one
// (we get all from index 0 to the selected one)
const monthsBeforeSelected = this.roundRobinMonths.slice(0, selectedMonth + 1);

// this is the months in round robin order - place the arrays together
const orderedMonths = monthsAfterSelected.concat(monthsBeforeSelected);

// push them all into the user timeline
orderedMonths.forEach(m => {
    this.userTimeline.push(m);
});
下面是JavaScript中函数的一个快速示例:

函数getRoundRobin(索引){ 施工月数=['1月'、'2月'、'3月'、'4月'、'Mai'、'6月'、'7月'、'8月'、'9月'、'10月'、'11月'、'12月']; 之后的常数=月数切片(指数+1,月数长度); 之前的常数=个月。切片(0,索引+1); 在.concat之后返回(在之前); }
console.log(getRoundRobin(8));//选择九月
您可以尝试切片

public pickDate($event: MouseEvent) {
    this.datePicker.selectDate(this.selectedWeddingDate, { openFrom: $event }).subscribe((selectedDate: Date) => {

        this.selecteDate = selectedDate ? moment(selectedDate) : null;

        // logic for creating personalized timeline
        let month = this.selectDate.month() + 1;
        this.userTimeline = this.roundRobinMonths.slice(month , 12).concat(this.roundRobinMonths.slice(0 , month));
        console.log(this.userTimeline);

    });
}

非常感谢你的回答!这正是我想要的wanted@DoubleH这将使用
切片
,而不是
拼接
,作为您的答案。您可能需要解释
slice
concat
如何组合以提供所需输出的逻辑。@JamesMonger。。很抱歉谢谢你纠正我。。我将很快更新我的帖子,并提供适当的解释