简体   繁体   中英

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=[];

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

I receive in my component index for a month that user selects ie 0, 1,.., 11.

I tried running a for loop like following:

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

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:

 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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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