简体   繁体   English

打字稿以循环方式将元素推入数组

[英]typescript push elements into an array in roundrobin fashion

I have an array: 我有一个数组:

public roundRobinMonths: any=['Jan', 'Feb', 'Mar', 'Apr', 'Mai', 'Jun', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

and another empty array: public userTimeline: any=[]; 另一个空数组: public userTimeline: any=[];

My user will select a month from a datepicker and if he selects say September then I want to add months starting from Oct until Sep in my userTimeline array ie Oct, Nov,..., Apl, Mai,..., Aug, Sep 我的用户将从日期选择器中选择一个月,如果他选择说9月,那么我想在我的userTimeline数组中添加从10月到9月的月份,即10月,11月,...,Apl,麦,...,8月,9月

I receive in my component index for a month that user selects ie 0, 1,.., 11. 我收到我选择的月份(即0、1,..,11)的组件索引。

I tried running a for loop like following: 我尝试运行如下所示的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;
    }

    });
}

This adds only one month. 仅增加一个月。 What could be the correct logic? 正确的逻辑是什么?

You can try slice 你可以尝试slice

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);

    });
}
// 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);
});

Here is a quick example of the function in JavaScript: 这是JavaScript函数的快速示例:

 function getRoundRobin(index) { const months = ['Jan', 'Feb', 'Mar', 'Apr', 'Mai', 'Jun', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; const after = months.slice(index + 1, months.length); const before = months.slice(0, index + 1); return after.concat(before); } console.log(getRoundRobin(8)); // select September 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM