Issue
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
}
                            Solution
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)
Answered By - Damzaky
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.