Saving Javascript value in key form

I want to save a value as a pair of keys and values ​​in javascript

So far i'm doing it like

var list_of_addressbook_entries = {}; list_of_addressbook_entries.guest_name = name ; for(key in list_of_addressbook_entries) { alert("key " + key + " has value " + list_of_addressbook_entries[key]); } 

In the above code, guest_name is a variable whose value comes from onclick
therefore, when I do the above, it shows me the result as

  key guest_name has value M 

it does not print the value of guest_name

I need a result like

 key guest_name_variable value has key M 

Please suggest me what to do here?

+4
source share
1 answer

If I understood correctly, you need to use the bracket [] syntax, otherwise it will not be interpreted as a variable:

list_of_addressbook_entries[guest_name] = name ;

+2
source

Source: https://habr.com/ru/post/1434809/


All Articles