简体   繁体   中英

combine 2 array objects typescript

I am trying to push one array object into another in typescript. Here is what i have:

days: DayDto[];

while (startsOn.toDate() < endsOn.toDate())
            {

                var newDate = startsOn.add(1, 'days');
                startsOn = moment(newDate);

                let d = this.getDayOfWeek(newDate.isoWeekday()) + newDate.date().toString();
                let w = this.getDayOfWeek(newDate.isoWeekday()) == "Sa" ? true : this.getDayOfWeek(newDate.isoWeekday()) == "Su" ? true : false;

               this.temp = new DayDto;

                this.temp.dayOfMonth = d;
                this.temp.weekEnd = w;
                this.temp.payPeriodEnd = "S31";

                //this.days.push(
                //    [
                //        new DayDto( d, w, "S31")
                //    ]
                //);
            }

So, I have a loop that while startsOn is less than endsOn, it loops through and gets the day of the week (Su) and the day of the month (21) and puts those into d and w. then those are put into the this.days array at the end of each loop. But i cannot get the logic correct for adding them to the array.

typescript supports es6, if you want to combine two array, you can do something like this

var arr1 = [0, 1, 2];
var arr2 = [3, 4, 5];
arr1.push(...arr2);

for detail information, https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Spread_operator , your question is unclear.

I don't know if I have fully understood your question.

If days is DayDto[] :

class DayDto { 
    constructor(
        public dayOfMonth: number,
        public weekEnd: number,
        public payPeriodEnd: string
    ) {}
}

var days: DayDto[] = [];

days.push(
    new DayDto(5, 5, "S31")
);

If days is DayDto[][] :

class DayDto { 
    constructor(
        public dayOfMonth: number,
        public weekEnd: number,
        public payPeriodEnd: string
    ) {}
}

var days: DayDto[][] = [];

days.push(
    [
        new DayDto(5, 5, "S31"),
        new DayDto(5, 5, "S31")
    ]
);

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