Sometimes, we want to put an array inside a JavaScript object.
In this article, we’ll look at how to put an array inside a JavaScript object.
How to put an array inside a JavaScript object?
To put an array inside a JavaScript object, we set its as a property’s value.
For instance, we write
const defaults = {
backgroundcolor: "#000",
color: "#fff",
weekdays: ["sun", "mon", "tue", "wed", "thu", "fri", "sat"],
};
to set the default.weekdays
property to the ["sun", "mon", "tue", "wed", "thu", "fri", "sat"]
array.
Conclusion
To put an array inside a JavaScript object, we set its as a property’s value.