structobject._childElements.push(element);
You're trying to modify the (non-existent) _childElements property of the Constructor function instead of the instance you created with new structobject.
Use this instead of structobject on that line.
It is conventional, in JavaScript, to use variables starting with capital letters for constructor functions.
var structobject = function(...) {
would be better written as:
var Structobject = function(...) {
or (since it is a constructor that makes objects):
var Struct = function(...) {
or (because named functions are easier to deal with in debuggers):
function Struct (...) {
Answer from Quentin on Stack OverflowArray in Node.js, how to work with it - javascript
How to add items to array in nodejs - javascript
How to create array of object in node.js - javascript
How to access node JS array in javascript? - JavaScript - SitePoint Forums | Web Development & Design Community
structobject._childElements.push(element);
You're trying to modify the (non-existent) _childElements property of the Constructor function instead of the instance you created with new structobject.
Use this instead of structobject on that line.
It is conventional, in JavaScript, to use variables starting with capital letters for constructor functions.
var structobject = function(...) {
would be better written as:
var Structobject = function(...) {
or (since it is a constructor that makes objects):
var Struct = function(...) {
or (because named functions are easier to deal with in debuggers):
function Struct (...) {
Use this instead of structobject
» npm install array
Check out Javascript's Array API for details on the exact syntax for Array methods. Modifying your code to use the correct syntax would be:
var array = [];
calendars.forEach(function(item) {
array.push(item.id);
});
console.log(array);
You can also use the map() method to generate an Array filled with the results of calling the specified function on each element. Something like:
var array = calendars.map(function(item) {
return item.id;
});
console.log(array);
And, since ECMAScript 2015 has been released, you may start seeing examples using let or const instead of var and the => syntax for creating functions. The following is equivalent to the previous example (except it may not be supported in older node versions):
let array = calendars.map(item => item.id);
console.log(array);
Here is example which can give you some hints to iterate through existing array and add items to new array. I use UnderscoreJS Module to use as my utility file.
You can download from (https://npmjs.org/package/underscore)
$ npm install underscore
Here is small snippet to demonstrate how you can do it.
var _ = require("underscore");
var calendars = [1, "String", {}, 1.1, true],
newArray = [];
_.each(calendars, function (item, index) {
newArray.push(item);
});
console.log(newArray);
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
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();
You can use Array.forEach
var myArray = ['1','2',3,4]
myArray.forEach(function(value){
console.log(value);
});
Run code snippetEdit code snippet Hide Results Copy to answer Expand
What you probably want is for...of, a relatively new construct built for the express purpose of enumerating the values of iterable objects... as distinct from for...in, which enumerates property names (presumably1 numeric indices in the case of arrays). Your loop displayed unexpected results because you didn't use the property names to get the corresponding values via bracket notation... but you could have.
const array = ["a","b","c","d"];
for (let value of array) {
console.log(value);
}
for (let propertyName in array) {
let value = array[propertyName];
console.log("propertyName: %o, value: %o", propertyName, value);
}
<!------------------------------------------------------------------------------><script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script><script>console.config({timeStamps:false,maximize:true})</script>
Run code snippetEdit code snippet Hide Results Copy to answer Expand
1 Unfortunately, someone may have added enumerable properties to the array or its prototype chain which are not numeric indices... or they may have assigned an index leaving unassigned indices in the interim range. The issues are explained pretty well here. The main takeaway is that it's best to loop explicitly from 0 to array.length - 1 rather than using for...in.
To address your comment (emphasis mine):
[...] why do I need to calculate the size of an array whereas the interpreter can know it[?]
Array.length is not calculated on the fly; it's updated whenever the length of the array changes. You're not going to see performance gains by avoiding it (apart from caching its value rather than repeatedly accessing the property). Here's a test of various constructs used to perform a sum operation on an array of 1,000 numeric elements (bigger bars are better).

Feel free to fork the test and try different inputs, methods, etc.
Can we process an array of million elements using node js? (assume if there are inner loops also)
My findings are
-
It may block the eventloop and freezes the server.
-
Memmory must be explicitly increased for the V8's heap.
Is that good practice ? Is my findings are valid ?
a simple solution:
const client_id = "system1";
const device_list = ['dev1' , 'dev2' , 'dev3']
const result = device_list.map(device => [client_id, device]);
The map method is part of the array prototype.
It loops through every item in an array, and applies the given callback to each item.
For more information, I find the Mozilla docs extremely useful!: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Another thing to note: map is NON-mutative, meaning it doesn't affect the array on which it is called, it only returns a new one
You could do something like this using map:
const result = device_list.map(device => [client_id, device])
This will map your existing device_list array to a new array, where each item consists of an array of client_id and device.
For..in is used to loop through the properties of an object, it looks like you want to loop through an array, which you should use either For Of, forEach or For
for(const val of rooms) {
console.log(val)
}
Using forEach() with your code example (room is an object) would look this:
temp1.rooms.forEach(function(element)
{
console.log(element)
});
Using For of with your code sample (if we wanted to return the rooms) looks like:
for(let val of rooms.room)
{
console.log(val.room);
}
Note: notable difference between For of and forEach, is For of supports breaking and forEach has no way to break for stop looping (without throwing an error).