I have an associative array in JS.
var array = {
'one' : 'first',
'two' : 'second',
'three' : 'third'
};
How can I add new pair in it
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.
{} 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.
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.
As mentioned earlier, JavaScript supports mixed arrays. That means you can create an array with numbers and strings or other objects.
array['newpair'] = 'new value';
or
array.newpair = 'newvalue';
This is quite a decent read on the subject.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With