Sometimes, we want to push an associative item into an array in JavaScript.
In this article, we’ll look at how to push an associative item into an array in JavaScript.
How to push an associative item into an array in JavaScript?
To push an associative item into an array in JavaScript, we use an object.
For instance, we write
const obj = {};
const name = "name";
const val = 2;
obj[name] = val;
console.log(obj);
to create the object obj
.
Then we add a property with key name
and value val
into obj
with
obj[name] = val;
Conclusion
To push an associative item into an array in JavaScript, we use an object.