0

I have a typescript class that contains an array attribute, i would like to create a copy of this object, i used Object.assign, but the array is not duplicated

class:

export interface IDeclaration {
    rechutes?: Rechute[];
    text: string;
}

export class Declaration implements IDeclaration {
    constructor(
        public rechutes?: Rechute[],
        public text ?: string,
    ) {}
}

export interface IRechute {
    dateDebut?: Moment;
    dateFin?: Moment;
}

export class Rechute implements IRechute {
    constructor(public dateDebut?: Moment, public dateFin?: Moment) {
        this.dateDebut = dateDebut ? dateDebut : null;
        this.dateFin = dateFin ? dateFin : null;
    }
}

copy :


const copy: IDeclaration = Object.assign({}, res.body, {
    text: transformMethodText(res.body.text),
    rechutes: transformMethodRechutes(res.body.rechutes)
});

transformMethodText(text: string) : string{
    return 'test : ' + text;
}

private transformMethodRechutes(rechute: Rechute): Rechute{
        return Object.assign({}, rechute, {
            dateDebut: rechute.dateDebut != null ? moment(rechute.dateDebut) : null,
            dateFin: rechute.dateFin != null ? moment(rechute.dateFin) : null
        });
    }

Array will always set to empty, when i try to push some new object no change is made.

How to copy my rechutes array inside the Object.assign function ?

Thank's

1 Answers1

-1

var deepCopy = JSON.parse(JSON.stringify(thingToCopy));

VorpalSword
  • 1,223
  • 2
  • 10
  • 25