0

is there a method to fill a local object in Angular 2 without deleting some attributes? For example, I have a A object:

export class A {
    first: string;
    second: string;
}

Then I get a response like this in B:

{
    "first": "bob"
}

Now I how to do something like that:

public a: A; // first and second are undefined
// Retrievethe response in b
this.a = b; // **Without doing fields for fields**

But b has no second attribute, so now a object has no more the second attribute but I have that it is undefined. I see that for forms there is patchvalue method, but it's valid only for forms...

Thanks

3 Answers 3

1

does this help

   export class A {
        first?: string;
        second?: string;
    }

    b = new A({
        "first": "bob"
    });

    public a = new A(); 

you can copy value one obj to anther via two way

    1. you can assign value directly 

 this.a = this.b;

    2.you can assign value using this method

       Object.assign(a, b);
Sign up to request clarification or add additional context in comments.

8 Comments

Hi, when I define a with public a: A; a is undefined, so with assign I get "Cannot convert undefined or null to object"... any workaround?
Thanks, in this simple case woks but not in my specific case where I have an array. With Object.assign(myArray, newArray); I don't know why but empty fileds are deleted.
can you create plunker?
Hi, I notice that the problem is in the constructor... more info in the code: plnkr.co/edit/oBVsqsJ50A6cCqpCtY7f?p=preview
its working well. what you trying to achieve? see updated plunk
|
0

If you have an existing instance of A then you can do

Object.assign(a, b);

And it will copy over only the properties defined in b. If you don't already have an instance of A but just want the properties to be defined as null before copying the values of B then have a function somewhere that creates a default instance of A which just assigns all its values to null.

Comments

0

Define as optional parameter and assign directly

define as interface not as class

.

export interface A {
  first?: string;
  second?: string;
}

Now you can assign directly even any of parameter missing

{
"first": "bob"
}

public a: A; 
this.a = b; 

5 Comments

Hi, when define public a: A; a is undefined, so I lost the second field...
"when define public a: A; a is undefined, so I lost the second field" whats that mean??
If I have:"export interface A { first?: string; second?: string; }". Now in your code you have write: public a: A; this.a = b; ". After the first line (public a: A;) a is undefined, so on the second line (this.a = b;) b overwrite the undefined a, I never had "second" field..
Assigning Interface will not change anything in the variable. Interface is just defenition about that variable this.a. It will be undefined unless until you assign something in this. This is what nature of JS
Why i asked you to mention in optional parameter means, Compiler will not give error on this assignment case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.