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

Adewuyi Kupolati
PLUS
Adewuyi Kupolati
Courses Plus Student 8,887 Points

Mixing and Matching Arrays and Objects

Need help Please. Thank you.

script.js
var objects = [
  {car: 'Volvo and Honda', number: 2}
];
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Objects</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
Anish Walawalkar
Anish Walawalkar
8,534 Points

Can you be a little more specific as to what the problem is. I do not see anything wrong with your creation of an array of objects.

2 Answers

What's your actual question?

Yes, you can mix objects and arrays; In fact, arrays are just special, numerically keyed objects - If you typeOf on it, an array will be an Object type. The key difference between arrays and objects is that arrays are declared as a set of comma separated values inside square brackets, ["value1", "value2"], while objects are declared with curly braces, keys, and values, {key1 : "value1" , key2 : "value2"}.

You can also have multi-dimensional objects and arrays; In other words, an array of arrays, an object of arrays, an array of objects, etc. etc. You declare them in the same way, just nested inside eachother. For example, an array of objects would look like:

var objects = [
  {key: 'keyValue1', answer: 'answerValue1'},
  {key: 'keyValue2', answer: 'answerValue2'},
  {key: 'keyValue3', answer: 'answerValue3'}
];

Hope that helps

Adewuyi Kupolati
PLUS
Adewuyi Kupolati
Courses Plus Student 8,887 Points

Thank you so much. I just had the question answered. I did not realize that the quotations were needed for the answers too. Thank you.

You're welcome. Yes, strings always go in quotes, while numbers never do.

Just some food for thought on forum etiquette... This would have been better as a comment, rather than an additional Answer. Also, is it possible to upvote and select my answer as Best Answer?