Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add key value-pair to a Object?

I want to update my Object by adding a more key-value pair.

Object options = {
  "first_name": "Nitish",
  "last_name" : "Singh"
}

after initializing the Object I want to add one more key and value. Is there any way to do this.

after adding one more key-value pair my object will look like this

options = {
  "first_name" : "Nitish",
  "last_name"  : "Singh"
  "middle_name": "Kumar"
}
like image 944
nitishk72 Avatar asked May 09 '18 03:05

nitishk72


People also ask

How do you add a key-value pair to an array of objects?

To add a key/value pair to all objects in an array:Use the Array. map() method to iterate over the array. On each iteration, use the spread syntax to add the key/value pair to the current object. The key/value pair will get added to all objects in the new array.


1 Answers

You can assign to a Map using the indexing operator

options['middle_name'] = 'Kumar';

{} is a Map literal to create a Map instance. The result allows you to use all methods of Map like remove

like image 106
Günter Zöchbauer Avatar answered Sep 29 '22 06:09

Günter Zöchbauer