Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
16,201 Points

Could you possibly link the code challenge to this post?

4 Answers

Thomas Fildes
Thomas Fildes
16,201 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
215,954 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 :)