I tried to create an array of objects but I can't make it work. Here is what I do:
var params = new Array();
console.log(params);
In the console, I see that params is indeed an array.
param = {
begindate: '2014-02-28',
begintime: '00:00:00',
enddate: '2014-02-28',
endtime: '23:59:59',
type: 'abs',
units: 'm3',
steps: 'none',
measureid: '1'};
params.push(param);
console.log(params);
now in the console, I see that params is an object :(.
How can I do that so I have an array of objects?
Thanks,
John.
2 Answers 2
In your browser's console window, you may be seeing:
[>Object]
You need to expand the output to see the array's objects. You may access the first index like so, which should output your param object:
console.log(params[0]);
Note I recommend referencing this to not use new Array() when possible:
What’s the difference between "Array()" and "[]" while declaring a JavaScript array?
Instead just initialize an array with var params = [];
Comments
Params is Array You can check it with:
console.log(params.constructor)
paramsis still an array,paramis an object.paramsis still an array. When you get something like this in the console:[Object { begindate="2014年02月28日", begintime="00:00:00", etc...}], the "Object" you're seeing is the objectparam, stored inside the arrayparams; your code already successfully creates an array of objects.