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": "test@gmail.com", 
    "name": "Testing", 
    "parentCustomerId": "myCustomerId3", 
    "phoneNumber": "1111111111", 
    "primaryContactUserId": "primaryContactUserId", 
    "status": "ACTIVE", 
    "xylemPrimaryContactId": "testing" 
}

2

{
    "address": "string",
    "childrenCustomerIds": ["testing"],
    "customerId": "BGroup",
    "email": "test@gmail.com",
    "name": "Testing",
    "parentCustomerId": "myCustomerId3",
    "phoneNumber": "1111111111",
    "primaryContactUserId": "primaryContactUserId",
    "properties": {},
    "status": "ACTIVE",
    "xylemPrimaryContactId": "testing"
}

1 Answers1

3
var obj={ 
    "address": "string", 
    "customerId": "BGroup", 
    "email": "test@gmail.com", 
    "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": {}});
mohit uprim
  • 5,226
  • 2
  • 24
  • 28
  • 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. – Anamika Badal Nov 14 '17 at 05:22
  • Object.assign() is ES6/Typescript feature only for more detail like clone using interface, please have a look to this one https://stackoverflow.com/questions/22885995/how-do-i-initialize-a-typescript-object-with-a-json-object – mohit uprim Nov 14 '17 at 07:03