0

I wanted to insert each keys from object to another object , I wanna add it to the payload.

I tried this one but it was insert as a whole object. What I want is to get the sample result below . Any ideas would be much appreciated.

const payload: requestPayload| any = {
          accountId: 1,
          keystoInsert
        };

#object that I wanna insert

keystoInsert = {
    "marketName": "Chicago",
    "roleUserFirstName": null,
    "roleUserLastName": null,
    "roleUserEmailAddress": null
}

#code snippet

 const payload: requestPayload | any = {
      accountId: 1,
    };

#expected output

{
    "accountId": 1,
    "marketName": "Chicago",
    "roleUserFirstName": null,
    "roleUserLastName": null,
    "roleUserEmailAddress": null
}
Mark Latin
  • 37
  • 6

1 Answers1

1

You can use spread on both objects:

const payload = {
  accountId: 1,
};

const keystoInsert = {
  "marketName": "Chicago",
  "roleUserFirstName": null,
  "roleUserLastName": null,
  "roleUserEmailAddress": null
}

const output = {
  ...payload,
  ...keystoInsert
}

console.log(output)
Damzaky
  • 6,073
  • 2
  • 11
  • 16