Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects Create an Array of Objects

David Donohue
David Donohue
1,110 Points

declaring objects in an array

Initially when the video introduces objects, when the object is declared, it is done so like this: var object = {key1: value1, key2: value2}, However, in the exercises/challenge when adding objects to an array, when I used that syntax like such: var array = [ var object1 = {key1: value1, key2: value2}, var object2 = {key1: value1, key2: value2}, var object3 = {key1: value1, key: value2}; it did not recognize the objects. I kept getting an error saying the array should have 3 values. When i constructed the array like the following it worked: var array = [ object1 = {key1: value1, key2: value2}, object2 = {key1: value1, key2: value2}, object3 = {key1: value1, key: value2}; Why is that?

2 Answers

Steven Parker
Steven Parker
231,007 Points

I'm surprised they didn't both cause a syntax error, but I suppose the significant difference is that a declaration doesn't return a value but an assignment does.

But you didn't need either, this syntax would have been correct for the challenge:

var array = [ {key1: value1, key2: value2},
              {key1: value1, key2: value2},
              {key1: value1, key2: value2} ];
David Donohue
David Donohue
1,110 Points

When I type your solution into the JavaScript console in Chrome, I get an Uncaught ReferenceError: value1 is not defined at <anonymous>:1:21

var array = [ {key1: value1, key2: value2}, {key1: value1, key2: value2}, {key1: value1, key2: value2} ];

However, when I type either of the following, it works; var array = [object1 = {name: 'david, age: 30}]; var array2 = [{name: 'john', age: 40}];

Steven Parker
Steven Parker
231,007 Points

This example is only to illustrate the format of the answer code, it's not the actual answer itself. I assumed that was what you were showing in your question also and was responding in kind.

It could, however, be converted into the answer by replacing all the "value1" and "Value2" references with some actual numeric or string values (or just by enclosing them in quotes). And of course "array" would be renamed "objects".

So are you saying the second example worked as is? That's even more surprising.

David Donohue
David Donohue
1,110 Points

Thanks for replying btw! I forgot to thank you in my reply!