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


why can't I just .push() the newTeachers array into the teachers const?

so in the solution video they used spread operators which we haven't learned about (great UX).

Based on the course, I thought that I can simply just push new items into the const array teachers and what I can't do is to the reassign the teachers array (which I didn't).

Why can't I just push? Why do I need to use the (so far hasn't learned) spread operators?

1 Answer

Louise St. Germain
Louise St. Germain
19,424 Points


You can push the newTeachers array into the teachers array in theory, but the issue is that it won't give you the result you want. In the example you're talking about (If it's the one I'm thinking of!), teachers is an array with a single object literal in it, and newTeachers has a similar format - an array with two object literals in it.

What the end goal should be is to have a teachers array with three object literals, one per teacher.

The trouble with directly pushing newTeachers onto teachers is that while it will let you do it, you will end up with the teachers array having only two items in it: one object literal (the original teacher), and one array, which itself contains two object literals. So in essence, the tail end of teachers suddenly becomes a 2D array. To avoid that, you'll need a way to push each of newTeachers object literals (and not the whole array structure) onto teachers.

I agree that the spread operator does suddenly come from nowhere here. However, you can do without it by using a regular for loop to cycle through the newTeachers array, and push each object separately onto teachers in this loop.

I hope this helps!