To create an array of objects in JavaScript, you can simply initialize an array and then add objects to it or create an empty array and then add objects to it using the push()
method.
Create an array of objects in JavaScript example
An array of objects in JavaScript create an array of objects by simply initializing an array and then adding objects to it.
<!DOCTYPE html>
<html>
<body>
<script>
let myArray = [
{ name: "John", age: 30 },
{ name: "Jane", age: 25 },
{ name: "Bob", age: 40 }
];
console.log(myArray)
</script>
</body>
</html>
Output:
You can also create an empty array and then add objects to it using the push()
method.
let myArray = [];
myArray.push({ name: "John", age: 30 });
myArray.push({ name: "Jane", age: 25 });
myArray.push({ name: "Bob", age: 40 });
Do comment if you have any doubts or suggestions on this Js array object topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version