Categories
JavaScript Answers

How to push an associative item into an array in JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *