I'm struggling to add(convert json to list of jsons) list to json. Here is the example:
config = {
existing_variable: 'example'
}
addToListing = (field, value, index=0) => {
config = {
...config,
listing: {
...config.listing,
[field]: value
}
}
}
addToListing('first', 1, 0)
addToListing('second', 2, 0)
addToListing('first', 3, 1)
console.log(config)
Result:
{
existing_variable: 'example',
listing:
{
first: 3,
second: 2
}
}
Expected result:
{
existing_variable: 'example',
listing:
[
{first: 1, second: 2},
{first: 3}
]
}
I've tried different approaches, but what I did wasn't suitable(e.g nested values or complete initialization at beggining).
Could you please help?