You need to push a newly created item to your array like below:

CoinArray.push(new Coin.Create('123)); 

On the other hand if you want to create an object with keys ids and values corresponding Coin objects, you should try this:

CoinDictionary = {};
CoinDictionary['123'] = new Coin.Create('123');

Note:

I think that you should refactor a bit the Coin if you want to use it as a constructor function:

function Coin(id){
    this.id = id;
}

Doing this change you can use it as below:

CoinArray.push(new Coin('123'));

function Coin(id){
    this.id = id;
}

var CoinArray = [];
CoinArray.push(new Coin('123'));
CoinArray.push(new Coin('456'));
CoinArray.push(new Coin('789'));

console.log(CoinArray);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Update

At the end I want to have array with object and for example I will take first el of array, and execute other method from Coin class.

For this purpose If I were you I would have gone with the creation of an object with keys the ids and values references to Coin objects:

function Coin(id){
    this.id = id;
}

Coin.prototype.start = function(){
    console.log("game with id "+this.id+" started.");
}

Coins = {}
Coins['123'] = new Coin('123');
Coins['456'] = new Coin('456');
Coins['789'] = new Coin('789');

Coins['456'].start();
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Answer from Christos on Stack Overflow
Top answer
1 of 2
2

You need to push a newly created item to your array like below:

CoinArray.push(new Coin.Create('123)); 

On the other hand if you want to create an object with keys ids and values corresponding Coin objects, you should try this:

CoinDictionary = {};
CoinDictionary['123'] = new Coin.Create('123');

Note:

I think that you should refactor a bit the Coin if you want to use it as a constructor function:

function Coin(id){
    this.id = id;
}

Doing this change you can use it as below:

CoinArray.push(new Coin('123'));

function Coin(id){
    this.id = id;
}

var CoinArray = [];
CoinArray.push(new Coin('123'));
CoinArray.push(new Coin('456'));
CoinArray.push(new Coin('789'));

console.log(CoinArray);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Update

At the end I want to have array with object and for example I will take first el of array, and execute other method from Coin class.

For this purpose If I were you I would have gone with the creation of an object with keys the ids and values references to Coin objects:

function Coin(id){
    this.id = id;
}

Coin.prototype.start = function(){
    console.log("game with id "+this.id+" started.");
}

Coins = {}
Coins['123'] = new Coin('123');
Coins['456'] = new Coin('456');
Coins['789'] = new Coin('789');

Coins['456'].start();
Run code snippetEdit code snippet Hide Results Copy to answer Expand

2 of 2
2
class Coin {
  constructor(id) {
    this._id = id;
  }
  start() {
    console.log('starting');
  }
}

CoinArray = [];
CoinArray['123'] = new Coin(123);
CoinArray['333'] = new Coin(333);
CoinArray['123'].start();

Also i would suggest managing id's that way:

let id = 0;
class Coin {
  constructor() {
    this._id = id++;
  }
  start() {
    console.log('starting');
  }
}

CoinArray = [];
CoinArray.push(new Coin());
CoinArray.push(new Coin());
CoinArray[0].start();
🌐
Example Code
example-code.com › nodejs › json_array_of_objects.asp
Node.js JSON: Array of Objects
Chilkat • HOME • Android™ • AutoIt • C • C# • C++ • Chilkat2-Python • CkPython • Classic ASP • DataFlex • Delphi DLL • Go • Java • Node.js • Objective-C • PHP Extension • Perl • PowerBuilder • PowerShell • PureBasic • Ruby • SQL Server • Swift • Tcl • Unicode C • ...
Discussions

Efficient way of iterating over a array of objects in javascript/node.js - Stack Overflow
With this approach not only you can tell if there is a person in the array with the specific name but you can also find the whole object with O(1). Take into account that this map will only index people by name if you want other criteria you need another map. More on stackoverflow.com
🌐 stackoverflow.com
node.js + Object Array - javascript
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... In my node app i have to construct an object array from another object array. More on stackoverflow.com
🌐 stackoverflow.com
May 24, 2017
Sending an array of objects in a node js post request
Share some code? More on reddit.com
🌐 r/node
14
0
November 22, 2024
Sending an array of objects in a node js post request
I am trying to send an array of objects in a post request but when I try to access the array with req.body it is just showing {object} and none of the objects content is being sent. Is there something wrong with my req… More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
2
0
November 22, 2024
🌐
Mixu
book.mixu.net › node › ch5.html
5. Arrays, Objects, Functions and JSON - Mixu's Node book
Objects are useful when you need to have named properties (like a hash), and you don't care about the order of the properties. The most common basic operations include iterating the properties and values of an Object, and working with arrays of Objects.
🌐
Java2s
java2s.com › Tutorials › Javascript › Node.js_Tutorial › 0080__Node.js_Arrays.htm
Node.js Tutorial - Node.js Arrays
Index of /Tutorials · Parent Directory · Android.zip · Android/ · AndroidImage/ · AngularJS/ · AngularJSDemo/ · Bootstrap/ · BootstrapDemo/ · CImage/
🌐
CodeMag
codemag.com › Article › 1405000 › Node.js-Succinctly
Learn to Use an Array Object in Node.js; an Excerpt from Note.js Succinctly
After creating an Array object, we can insert data. Use [] with index if you want to assign the value. array[0] = 3; array[1] = 5; array[2] = 12; array[3] = 8; array[4] = 7; You can also use the push() function to insert data. ... To access array data, you can use [] with data index parameter.
🌐
Quora
quora.com › How-do-you-map-an-array-of-objects-in-Node-js
How to map an array of objects in Node.js - Quora
Answer (1 of 3): There is a method called .map that can be used on arrays of objects. This method calls a function on each element of the array and returns a new array with the results. For example, if you have an array of objects like this: [code]const arr = [ { name: 'Alice' }, { name: 'Bob' ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript - MDN Web Docs
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.
Find elsewhere
Top answer
1 of 4
9

You can use the array filter or find methods

ArrPerson.find(p=>p.name=='john')
ArrPerson.filter(p=>p.name=='john')

The find method searches the array from the beginning and stops when it finds one element that matches. In the worst case where the element that is being searched is the last in the array or it is not present this method will do O(n).This means that this method will do n checks (n as the length of the array) until it stops.

The filter method always does O(n) because every time it will search the whole array to find every element that matches.

Although you can make something much more faster(in theory) by creating a new data stucture.Example:

var hashmap = new Map();
var ArrPerson = [];
ArrPerson.push(new Person("john",12,"M1"));
hashmap.set("john",true);

This ES6 Map will keep an index of the whole array based on the names it contains. And if you want to see if your array contains a name you can do:

hashmap.has('john')//true

This approach will do O(1). It will only check once on the map to see if this name exists in your array. Also, you can track the array indexes inside the map:

var index = ArrPerson.push(new Person("john",12,"M1"));
var map_indexes = hashmap.get("john");
if(map_indexes){
  map_indexes.push(index-1);
  hashmap.set("john",map_indexes);
}else{
  hashmap.set("john",[index-1]);
}
map_indexes = hashmap.get("john"); //an array containing the ArrPerson indexes of the people named john
//ArrPerson[map_indexes[0]] => a person named john
//ArrPerson[map_indexes[1]] => another person named john ...

With this approach not only you can tell if there is a person in the array with the specific name but you can also find the whole object with O(1). Take into account that this map will only index people by name if you want other criteria you need another map. Also keeping two data structures synchronized is not easy(deleting one element from array should also be deleted from the map, etc)

In conclusion, as always increasing speed ends sacrificing something else, memory and code complexity in our example.

2 of 4
6
  • use ES5 array methods like map ,filter ,reduce etc
  • use forEach
  • native for loop

Example : filter , map, reduce etc. methods iterate over each item or object in an array,

ArrPerson.filter(function(item){
       console.log(item)
   });

forEach : also iterates over each item/object in an array

 ArrPerson.forEach(function(key,value){
     console.log(key);
     console.log(value)
  })

The question said huge array , so

native for loop is way faster than any of the above and caching the length can improve by a few ms (milliseconds).

https://jsperf.com/native-map-versus-array-looping

for(var i = 0, len = ArrPerson.length; i < len; i++){

}
🌐
Reddit
reddit.com › r/node › sending an array of objects in a node js post request
r/node on Reddit: Sending an array of objects in a node js post request
November 22, 2024 -

I am trying to send an array of objects in a post request but when I try to access the array with req.body it is just showing {object} and none of the objects content is being sent. Is there something wrong with my request?

here is my request:

Post http://localhost:3000/users/newOrder Content-Type: application/json

{ "firstName": "Craig", "lastName": "Mar", "address1": "10 a st", "address2": "apt 2", "email": "c@yahhoo.com", "town": "red bank", "state": "nj", "zip": "07701", "shippingCost": 12.00, "orderCost": 25.00, "productsArray": [{ "id": "4a482549-79aa-4798-8a3b-a19746cd645a", "title": "Microsoft Xbox X/S Wireless Controller Robot White", "price": 57, "quantity": 1, "total": "57.00", "image": "https://storage.googleapis.com/fir-auth-1c3bc.appspot.com/1692255251854-xbox.jpg" }, { "id": "827ab3f1-b5d5-436a-93cc-7a4a28aefd3b", "title": "Sony WH-1000XM3 Bluetooth Wireless Over Ear Headphones with Mic (Silver)", "price": 773, "quantity": "2", "total": "1546.00", "image": "https://storage.googleapis.com/fir-auth-1c3bc.appspot.com/1692947383286-714WUJlhbLS.SL1500.jpg" }

]

}

a console log of the req.body is the following:

{ firstName: 'Craig', lastName: 'Mar', address1: '10 a st', address2: 'apt 2', email: 'c@yahhoo.com', town: 'red bank', state: 'nj', zip: '07701', shippingCost: '12', orderCost: '25', productsArray: [ '[object Object]', '[object Object]' ] } if i try to access the values of the objects in the products array with dot notation i am getting undefined any ideas where i am going wrong? I have also tried a curl request but get the same result.

🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Sending an array of objects in a node js post request - Curriculum Help - The freeCodeCamp Forum
November 22, 2024 - I am trying to send an array of objects in a post request but when I try to access the array with req.body it is just showing {object} and none of the objects content is being sent. Is there something wrong with my request? here is my request: Post http://localhost:3000/users/newOrder Content-Type: application/json { "firstName": "Craig", "lastName": "Mar", "address1": "10 a st", "address2": "apt 2", "email": "c@yahhoo.com", "town": "red bank",...
🌐
Stack Overflow
stackoverflow.com › questions › 47971131 › array-of-objects-or-object-of-objects-in-nodejs-for-in-memory-simple-data-base
node.js - Array of objects or Object of objects in NodeJS for in memory simple data base - Stack Overflow
December 25, 2017 - What do you think about this approach? ... from array to object?, can you suggest improvement or libraries that already do this? ... It seems obvious that if you have unique ids you should store them in an object keyed to id. I’m not sure why you are worrying about multiple threads in a Node app, ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-access-array-of-objects-in-javascript
How to Access Array of Objects in JavaScript ? - GeeksforGeeks
It directly retrieves individual properties, allowing precise access to the data inside objects at specific indices. ... Example: The code below demonstrates how we can use the DOT notation along with the brackets notation to access the elements of the array of objects:
Published: July 23, 2025
🌐
freeCodeCamp
freecodecamp.org › news › javascript-array-of-objects-tutorial-how-to-create-update-and-loop-through-objects-using-js-array-methods
JavaScript Array of Objects Tutorial – How to Create, Update, and Loop Through Objects Using JS Array Methods
May 14, 2020 - Make sure to always add the case for zero when the compared value of both objects is the same to avoid unnecessary swaps. Array.every and Array.some come handy when we just need to check each object for a specific condition.
🌐
W3Schools
w3schools.com › js › js_arrays.asp
JavaScript Arrays
Because of this, you can have variables of different types in the same Array. You can have objects in an Array. You can have functions in an Array.
🌐
Reddit
reddit.com › r/node › how to get unique set of array of objects in javascript
r/node on Reddit: how to get unique set of array of objects in javascript
June 3, 2020 -

i have an array

friendList = [ 
                {id: 1, fName: 'Sam', lName: 'Brian', profilePic: 'sam.jpg'}, 
                {id: 2, fName: 'Dam', lName: 'Nary', profilePic: 'dam.jpg'}, 
                {id: 3, fName: 'Jam', lName: 'kent', profilePic: 'jam.jpg'}, 
                {id: 1, fName: 'Sam', lName: 'Brian', profilePic: 'sam.jpg'}, 
                {id: 3, fName: 'Jam', lName: 'kent', profilePic: 'jam.jpg'}, 
             ]

how can i get a unique array of this friendList.. such that the result is :

friendList = [ 
               {id: 1, fName: 'Sam', lName: 'Brian', profilePic: 'sam.jpg'}, 
               {id: 2, fName: 'Dam', lName: 'Nary', profilePic: 'dam.jpg'}, 
               {id: 3, fName: 'Jam', lName: 'kent', profilePic: 'jam.jpg'} 
             ]

the solutions i found on the internet are like for primitive type arrays. how to do that with array of objects

🌐
Codecademy
codecademy.com › learn › paths › web-development › tracks › web-dev-js-arrays-loops-objects › modules › learn-javascript-objects › cheatsheet
JavaScript: Arrays, Loops, and Objects: Objects Cheatsheet | Codecademy
Since this refers to the enclosing object, the value of the data property is accessed as expected, returning "abc". ... JavaScript getter and setter methods are helpful in part because they offer a way to intercept property access and assignment, and allow for additional actions to be performed before these changes go into effect.