Shilpa
Published in : 2022-03-06
My object literals:
{"k1": "v1", "k2":"v2"}
I want to add another key to the same object, how do I add another key {"k3": “v3”} to this object?
Join our community and get the chance to solve your code issues & share your opinion with us
Sign up Now
Mohamed Atef Date : 2022-03-06
Best answers
51
Best answers
51
You can add a new object using
or you can merge two objects using
Good luck
Shilpa Date : 2022-03-06
Best answers
10
Best answers
10
Using your 2nd approach,
Will other two objects destroy automatically? I just worried about performance if it is optimized solution or not when the code goes live?
Rakshit Date : 2022-03-06
Best answers
34
Best answers
34
Your input is,
With optimized solutions, you can use any approach as given below.
2. If you are sure about key is already existing, you can use this approach.
3. If you want to manage your object automatically with Object.assign method, use this approach. It is core utility provided using "Object" - itself. Visit here [Mozilla] to explore more about assign().
The above solutions will work to answer your questions.