Anamika Badal
Anamika Badal

Reputation: 23

How to add an object and array in an existing object using typescript or angular

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"
}

Upvotes: 0

Views: 2751

Answers (1)

mohit uprim
mohit uprim

Reputation: 5344

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": {}});

Upvotes: 3

Related Questions