简体   繁体   English

结合 2 个数组对象打字稿

[英]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.所以,我有一个循环,当startsOn 小于endsOn 时,它循环遍历并获取星期几(Su)和月份中的某天(21)并将它们放入d 和w。 then those are put into the this.days array at the end of each loop.然后在每个循环结束时将它们放入 this.days 数组中。 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 typescript 支持 es6,如果你想组合两个数组,你可以这样做

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.有关详细信息, 请访问 https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Spread_operator ,您的问题不清楚。

I don't know if I have fully understood your question.我不知道我是否完全理解你的问题。

If days is DayDto[] :如果daysDayDto[]

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[][] :如果daysDayDto[][]

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

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

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