Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS associative arrays: add new pair

I have an associative array in JS.

var array = {
    'one' : 'first',
    'two' : 'second',
    'three' : 'third'
};

How can I add new pair in it

like image 856
Artur Keyan Avatar asked Aug 04 '11 05:08

Artur Keyan


People also ask

How do I add an element to an associative array?

Use the array_merge() Function to Add Elements at the Beginning of an Associative Array in PHP. To add elements at the beginning of an associative, we can use the array union of the array_merge() function.

What is {} and [] in JavaScript?

{} is shorthand for creating an empty object. You can consider this as the base for other object types. Object provides the last link in the prototype chain that can be used by all other objects, such as an Array . [] is shorthand for creating an empty array.

Does JS support associative array?

JavaScript does not support associative arrays. You should use objects when you want the element names to be strings (text). You should use arrays when you want the element names to be numbers.

Can JavaScript arrays have mixed types?

As mentioned earlier, JavaScript supports mixed arrays. That means you can create an array with numbers and strings or other objects.


1 Answers

array['newpair'] = 'new value';

or

array.newpair = 'newvalue';

This is quite a decent read on the subject.

like image 69
user1122345 Avatar answered Sep 28 '22 05:09

user1122345