Question:
Am stuck trying to add a function to add a new object to a const in javascriptNormally a simple:
Answer:
Since it is objects inside objects, you can use[name][mobile]
to access the mobile
property of the corresponding name
. When creating a new name
, you have to use if-else conditions to first check if it exists.
Note that [numberType]
is used to create a dynamic key (where it creates a key with name as value of numberType
).If you have better answer, please add a comment about this, thank you!