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

Object literals inside array literals - what am I doing wrong?

I am trying to put an object literal inside an array literal. Here is my code:

var objects = [ {property1: "value1", "value2"}, {property2: "value3", "value4"}, {property3: "value5", "value6"} ];

I don't understand the error ("Task 1 is no longer passing.") What am I doing wrong?

Thomas Fildes
Thomas Fildes
22,687 Points

Could you possibly link the code challenge to this post?

4 Answers

Thomas Fildes
Thomas Fildes
22,687 Points

Hi Rachel,

Basically the task asks you to create 3 objects inside an array and each object is to have a pair which is 2 properties and the corresponding values for them. So what your code contains isn't a pair because you have multiple values (not one value per property). See the following code below that I have conjured up to pass the challenge and to show you what I mean if you are unsure:

var objects = [
  {
    name: "Steven",
    age: "23"
  },

  {
    name: "Stephen",
    age: "25"
  },

  {
    name: "Stefan",
    age: "27"
  }

];
Steven Parker
Steven Parker
230,929 Points

Objects are composed of property/value pairs. You can only have one value associated with each property.

But I wrote {property1: "value1", "value2"} -- isn't that two different values?

Julien riera
Julien riera
14,665 Points

Yeah,

Otherwise, maybe adding square brackets ([]), for instance, before and after your values list could also solve this.

Julien riera
Julien riera
14,665 Points

Hello,

Try one of this :

var objects = [ {property1: "value1"}, {property2: "value3"}, {property3: "value5"} ];

Or

var objects = [ {property1: ["value1", "value2"]}, {property2: ["value3", "value4"]}, {property3: ["value5", "value6"]} ];

Or, the one you should do to me

var objects = [ {property1 : "value1", property2: "value2"}, {property3: "value3", property4: "value4", property5: "value5"} ];


To add several values to a property, thus more than one, you have to use an array or an object. So what do I suggest here : 1) Only 1 value per object, which should work fine. 2) Wrap your values with [] to make one array of several values for one property. (Here an object) 3) each pair of {} means one object containing several properties separated by a coma. Once again, this is the solution to me here.

One more thing, just to be sure, your task1 should look like : var objects = [];

Let's stay tuned :)