There are quite a few similar questions but I couldn't get their answers to work.
let obj = {};
const key;//a string
const value;//a string
obj[key].push(value);
Obviously this doesn't work but I don't know how to do this. I want it to add a new key and value if it doesn't exist, but if it does exist it should append it to the end of the values for that particular key. ie like the normal push action with arrays.
Expected behaviour:
key = 'hello'
value = 'thanks'
obj = {'hello' : ['thanks']}
key = 'goodbye'
value = 'ok'
obj = {'hello' : ['thanks'], 'goodbye' : ['ok']}
key = 'hello'
value = 'why'
obj = {'hello' : ['thanks','why'], 'goodbye' : ['ok']}
The value 'why' is appended to the end for key 'hello'.
EDIT: All values are put into arrays.
obj = {'hello' : 'thanks'}and notobj = {'hello' : ['thanks']}if you know in advance thathellois going to hold more than one value?obj.add('key', [1, 2])andobj.add('key', 1); obj.add('key', 2)produces the same object, but they must be treated differently when you try to add one more time.