I'm looking to dynamically merge/insert objects to refactor a bunch of API request object types and I can't seem to find a clear answer.
let isThingTrue = true;
let dynamicObjectItem1 = {"foo": "bar"};
let dynamicObject = {
"item1": "stuff here",
"item2": "stuff here",
isThingTrue ? dynamicObject : <add nothing>
"item3": "stuff here
};
If isThingTrue is true, then object looks like:
{
"item1": "stuff here",
"item2": "stuff here",
"foo": "bar",
"item3": "stuff here
}
If isThingTrue is false, then object looks like:
{
"item1": "stuff here",
"item2": "stuff here",
"item3": "stuff here
}
Is this possible? Adding a value like this doesn't work.
let dynamicObjectItem1 = {"foo": "bar"};
let dynamicObjectStart = {
"item1": "stuff here",
"item2": "stuff here",
dynamicObjectItem1,
"item3": "stuff here"
};
Which produces:
{
"item1": "stuff here",
"item2": "stuff here",
"dynamicObjectItem1": {
"foo": "bar"
}
"item3": "stuff here"
};
Is there a clean way to make this work without a bunch of Object.assigns?