0

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.

user2040597
  • 469
  • 3
  • 8
  • 21
  • 2
    `params` is still an array, `param` is an object. – p.s.w.g Mar 01 '14 at 17:40
  • Yes, `params` is 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 object `param`, stored inside the array `params`; your code already successfully creates an array of objects. – Serlite Mar 01 '14 at 17:44

2 Answers2

1

Params is Array You can check it with:

console.log(params.constructor)
Eliran
  • 26
  • 1
  • 1
1

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 = [];

Community
  • 1
  • 1
wongcode
  • 123
  • 6