0

I am a beginner in angular 4 and using form builder,On ngSubmit I am creating an object containing the data of form fields. However before passing that object as a payload i need to add another object and an array inside the form object, I am wracking my brains but couldn't able to achieve it.

As you can see below 1 is the object that i am getting from form builder. and 2. is the final object expected and i need to add "childrenCustomerIds" and "properties" respectively in the object after form builder object is created, but i have no clue how to add that.

Any help would be appreciated..

thanks in advance..!!!

1

{ 
    "address": "string", 
    "customerId": "BGroup", 
    "email": "[email protected]", 
    "name": "Testing", 
    "parentCustomerId": "myCustomerId3", 
    "phoneNumber": "1111111111", 
    "primaryContactUserId": "primaryContactUserId", 
    "status": "ACTIVE", 
    "xylemPrimaryContactId": "testing" 
}

2

{
    "address": "string",
    "childrenCustomerIds": ["testing"],
    "customerId": "BGroup",
    "email": "[email protected]",
    "name": "Testing",
    "parentCustomerId": "myCustomerId3",
    "phoneNumber": "1111111111",
    "primaryContactUserId": "primaryContactUserId",
    "properties": {},
    "status": "ACTIVE",
    "xylemPrimaryContactId": "testing"
}
2
  • could you post your code for building the form ? Commented Nov 13, 2017 at 11:26
  • Do you need to add these fields in the form, OR after submitting form? It's a bit unclear... Commented Nov 13, 2017 at 11:37

1 Answer 1

3
var obj={ 
    "address": "string", 
    "customerId": "BGroup", 
    "email": "[email protected]", 
    "name": "Testing", 
    "parentCustomerId": "myCustomerId3", 
    "phoneNumber": "1111111111", 
    "primaryContactUserId": "primaryContactUserId", 
    "status": "ACTIVE", 
    "xylemPrimaryContactId": "testing" 
}

Add "childrenCustomerIds": ["testing"]

Object.assign(obj, {"childrenCustomerIds": ["testing"]});

similarly you can add "properties": {}

Object.assign(obj, {"properties": {}});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the solution however I think this approach is ES5 approach but I could see in ES6/Typescript there is a concept of interface which I am not able to understand completely. I would really appreciate if you have any ES6/Typescript solution.
Object.assign() is ES6/Typescript feature only for more detail like clone using interface, please have a look to this one stackoverflow.com/questions/22885995/…

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.