Aymen Kanzari
Aymen Kanzari

Reputation: 2023

replace object by another object

i have an organization object that contains and address object, i want to edit the address object with the values that i get from the form, then i want to change the address object that is in the organisation object with the new object address, so i used Object.assign, but it always returns the old address

export class Organization {
    public address?: Address;
    public telecoms?: Telecom[];
    public metier?: Dictionary;
}

export class Address {
    public zipCode?: Dictionary;
    public country?: Dictionary;
    public city?: string;
}

export class Dictionary {
    public id?: number;
    public code?: string;
    public label?: string;
}

this.organizationService.getOneById(this.id).subscribe((organization: Organization) => this.organization= organization });

const address = this.editForm.get("address").value;

console.log(JSON.stringify({ address })); // {"address":{"country":{"id":2},"zipCode":{"id":1}}} 

let organiationToSave: Organization = this.organization;

Object.assign({}, organiationToSave , JSON.stringify({ address }));

console.log(organiationToSave); 

Upvotes: 1

Views: 785

Answers (1)

Nurbol Alpysbayev
Nurbol Alpysbayev

Reputation: 21931

Object.assign({}, organiationToSave , JSON.stringify({ address }));

console.log(organiationToSave);

In the above code, you are assigning values to a new object {}, then you log organiationToSave. This makes no sense.

Maybe you want to do this instead?

const obj = Object.assign({}, organiationToSave , JSON.stringify({ address }));

console.log(obj);

Or if you want to update organiationToSave, do this:

Object.assign(organiationToSave , JSON.stringify({ address }));

console.log(organiationToSave);

Upvotes: 2

Related Questions