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

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Combining Object Literals with Array Literals

I've just finished the first part of Dave's challenge. And I was almost there... I'm reay to move on but I just wondered. Why do we not give name for each object in the array literal

var students [

  student1 = {
      name: "Jonnie",
      age: 14,
      track: "Web Design",
      achievements: 54,
      points: 45345

  },

  student2 = {

      name: "Jonnie",
      age: 45,
      track: "PHP",
      achievements: 54,
      points: 45345
  },

Dave's solution was more like this.

var sudents = [
{
      name: "Jonnie",
      age: 34,
      track: "Front End Track",
      achievements: 54,
      points: 45345},
},

{
      name: "Dave",
      age: 25,
      track: "Front End Track",
      achievements: 54,
      points: 45345
}

]

(I surreptitiously added an age key.)

Why is this? I'm pretty sure that if you were creating an object on it's own you'd give it a name. :-) Thanks.

2 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hey Jonathan,

Interesting question! The way I see it, your solution is almost analogous to an associative array or dictionary, while Dave's is a simple array. The use case will define which solution is better.

Best,

Greg

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Thanks Greg, so I guess both solutions would work and I'd maybe do best to follow Dave's model? :_

Greg Kaleka
Greg Kaleka
39,021 Points

Yeah, I'm not sure what context this is in, but if it's a course, it's probably best to just use the method Dave is using so you can follow along more easily :)