Use array.push() to add an item to the end of the array.

var sample = new Array();
sample.push(new Object());

To do this n times use a for loop.

var n = 100;
var sample = new Array();
for (var i = 0; i < n; i++)
    sample.push(new Object());

Note that you can also substitute new Array() with [] and new Object() with {} so it becomes:

var n = 100;
var sample = [];
for (var i = 0; i < n; i++)
    sample.push({});
Answer from Daniel Imms on Stack Overflow
🌐
Reddit
reddit.com › r/javascript › array of objects, best way to do it.
r/javascript on Reddit: Array of objects, best way to do it.
November 9, 2018 -

So I made an array with objects. For example here, let's use people

    var people = [
        {
            id: 0,
    
            name: "John Doe",
    
            age: 47
        },
        {
            id: 1,
    
            name: "Jane Doe",
    
            age: 88
        },
        {
            id: 2,
    
            name: "Mason Louis",
    
            age: 17
        }
    ];

so now I can use people[1].name etc for accessing the data on each of these people. Someone just saw my code and said "That looks like total S**T".

What is the proper way to do this? I was thinking this way was fine, especially since it resembles JSON

EDIT: Would it be better to create a constructor and fill an array with instances?

Discussions

javascript - Declaring array of objects - Stack Overflow
If you want all elements inside an array to be objects, you can use of JavaScript Proxy to apply a validation on objects before you insert them in an array. More on stackoverflow.com
🌐 stackoverflow.com
What is the best way to create an array of objects in Javascript? - Stack Overflow
This would be an array of objects. var cups = [{color:'Blue',size:'Large',type:'Mug'},{color:'Red',size:'Small',type:'Glass'}] ... Sign up to request clarification or add additional context in comments. ... Perfect! Exactly what I was looking for. 2016-12-20T23:13:59.083Z+00:00 ... "W3" is very misleading, please reference either the language specification or MDN for examples ... More on stackoverflow.com
🌐 stackoverflow.com
java - Object of arrays or array of objects? - Game Development Stack Exchange
I'm making a management sim game, something along the lines of Roller Coaster Tycoon. I want to know what the best way to structure my world objects is so to maximise performance. Let's say I have... More on gamedev.stackexchange.com
🌐 gamedev.stackexchange.com
July 8, 2013
Array of objects, best way to do it.
IMO, when people say "Your code looks like shit" and don't offer to help improve it, their opinion doesn't mean much. They aren't trying to help you, they're trying to bolster their own ego. More on reddit.com
🌐 r/javascript
31
35
November 9, 2018
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript | MDN
July 28, 2026 - The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name, and has members for performing common array operations. In JavaScript, arrays aren't primitives but are instead Array objects with the following core characteristics:
🌐
daily.dev
daily.dev › home › blog › webdev › create array of objects javascript: a beginner's guide
Create Array of Objects JavaScript: A Beginner's Guide | daily.dev
May 25, 2026 - Here, we have a mix: two fruits by name, a true/false value, and an object (think of it as a box with more info inside). ... Learning about arrays is like learning to organize your shopping list better. They can hold all sorts of things, even lists within lists! This makes them a key part of using JavaScript.
🌐
W3Schools
w3schools.com › js › js_arrays.asp
JavaScript Arrays
Arrays are a special type of objects. The typeof operator in JavaScript returns "object" for arrays. But, JavaScript arrays are best described as arrays. Arrays use numbers to access its "elements". In this example, person[0] returns John:
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-access-array-of-objects-in-javascript
How to Access Array of Objects in JavaScript ? - GeeksforGeeks
It applies a function to each object, returning a new array with modified data or specific properties from the original objects. ... Example: In this example we uses map instead of forEach to iterate over objArr.
Published: July 23, 2025
🌐
Eloquent JavaScript
eloquentjavascript.net › 04_data.html
Data Structures: Objects and Arrays :: Eloquent JavaScript
Inside the braces, you write a list of properties separated by commas. Each property has a name followed by a colon and a value. When an object is written over multiple lines, indenting it as shown in this example helps with readability. Properties whose names aren’t valid binding names or valid numbers must be quoted: let descriptions = { work: "Went to work", "touched tree": "Touched a tree" }; This means that braces have two meanings in JavaScript.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Arrays
Please think of arrays as special structures to work with the ordered data. They provide special methods for that. Arrays are carefully tuned inside JavaScript engines to work with contiguous ordered data, please use them this way. And if you need arbitrary keys, chances are high that you actually require a regular object {}.
🌐
Nfriedly
nfriedly.com › techblog › 2009 › 06 › advanced-javascript-objects-arrays-and-array-like-objects
Advanced Javascript: Objects, Arrays, and Array-Like objects
Javascript objects and arrays are both incredibly useful. They're also incredibly easy to confuse with each other. Mix in a few objects that look like arrays and you’ve got a recipe for confusion! We're going to see what the differences between objects and arrays are, how to work with some of the common array-like objects, and how to get the most performance out of each.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-access-properties-from-an-array-of-objects-in-javascript
How to Access Properties from an Array of Objects in JavaScript
February 29, 2024 - Here is an example of accessing the first element from the first array in the nestedArray: const nestedNumberArray = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ]; console.log(nestedNumberArray[0][0]); // 1 ... To access the first element from this array, ...
🌐
Esri Community
community.esri.com › t5 › arcgis-javascript-maps-sdk-questions › javascript-array-of-objects-sorting-how-to-sort-by › td-p › 1329511
JavaScript Array of Objects Sorting: How to Sort by a Specific Object Property
December 13, 2023 - You have to use the array.sort method, using a compare function to define the sort order. Take a look at this section of the documentation that explains different ways of sorting arrays of objects. In this example, a simpler sort function uses the locateCompare method to return the sorted titles.
🌐
Twine
twinery.org › questions › 44567 › how-to-make-an-array-of-objects
How to make an array of objects
January 31, 2019 - twine2 sugarcube2-21 sugarcube2 ... javascript css twine2-1-3 sugarcube variable variables macro twine links twine1 if sugarcube2-28-2 harlowe2 audio html array image snowman passages passage savegame inventory music arrays saves link time sugarcube2-25-0 textbox harlowe1-2-4 error text array-objects set images ...
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Indexed_collections
Indexed collections - JavaScript | MDN
2 weeks ago - An array is an ordered list of values that you refer to with a name and an index. For example, consider an array called emp, which contains employees' names indexed by their numerical employee number. So emp[0] would be employee number zero, emp[1] employee number one, and so on.
Top answer
1 of 3
15

The common terminology is "structure of arrays" (SOA) and "array of structures" (AOS) which come from C and is most often seen in terms of SIMD work.

Typically, the AOS approach is faster, if used appropriately, but SOA tends to be easier to work with (and hence optimizes for the more important quality - development time).

SOA, especially in Java, means that your data can remain tightly packed in memory. You can iterate over properties and expect the CPU cache and such to remain happy. With AOS, especially in Java, every object ends up allocated "somewhere" in memory. Iterating over objects could potentially thrash your CPU cache pretty heavily.

In the end, I would take whichever approach you find easiest to use. Your development time is far more valuable than whether your game supports 10 year old PCs or only 9 year old PCs (you're very unlikely to be doing anything htat needs the latest hardware).

2 of 3
4

There's no reason you can't have both, using the Facade pattern to translate from one interface to the other underlying representation. For example, using Sean's SOA/AOS terms:

SOA facade

class PeopleFacade {
    Person persons[5000];
    getThirst(int i) { return persons[i].thirst; }
}

AOS facade

class People { int thirsts[5000]; } people;
class PersonFacade {
    int i;
    getThirst() { return people.thirsts[i]; }
}

This way you can freely choose between a form you are comfortable with using, as a developer interface, vs whatever's best as an implementation for whatever reason, including efficiency/cache reasons.

Another advantage to the facade is that it leads very naturally to the Flyweight pattern, where you use an interface to represent much more persons than are actually in memory. For example, perhaps you have robotic patrons which are never thirsty; then you can put that special case into your PersonFacade, and users of that interface never have to know about robots:

class People { int nonRobotThirsts[1000]; } people;
class PersonFacade {
    int i;
    bool isRobot;
    getThirst() {
        if (isRobot)
            return 0;
        else
            return people.nonRobotThirsts[i];
    }
}

... or using a more OO approach, you'd have a separate Robot class which acts exactly like a Person except for getThirst().

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › from
Array.from() - JavaScript | MDN
For example, if a subclass of Array inherits the from() method, the inherited from() method will return new instances of the subclass instead of Array instances. In fact, the this value can be any constructor function that accepts a single argument representing the length of the new array. When an iterable is passed as items, the constructor is called with no arguments; when an array-like object ...
🌐
Scaler
scaler.com › home › topics › what is an array of objects in javascript?
What is an Array of Objects in JavaScript? - Scaler Topics
April 2, 2024 - Here, we get the native code as ... the javascript engine responsible for creating the Array prototype. 2. Length- It returns the length of elements in an array i.e. the number of objects present in the array. ... Above, we use the length properties on the myArr array of objects and also print it on the console. As result, we print 2 on the console. 3. Prototype- Array Prototype property helps to add new methods into the Array, for example- adding all ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › find
Array.prototype.find() - JavaScript | MDN
2 weeks ago - If you need to find if any element satisfies the provided testing function, use some(). If you need to find all elements that satisfy the provided testing function, use filter(). const array = [5, 12, 8, 130, 44]; const found = array.find((element) => element > 10); console.log(found); // Expected ...