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.

2
  • 2
    params is still an array, param is an object. Commented Mar 1, 2014 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. Commented Mar 1, 2014 at 17:44

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

1

Params is Array You can check it with:

console.log(params.constructor)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.