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
Torger Angeltveit
11,228 PointsHow do i get the number of how many objects my array holds?
Hi, I have an object named "cart"
everytime i push a button i add a new object with different strings to the array named "cart"
I want to return the total number of how many different objects i have inserted to the cart. but cart.length; return the number of strings and not the number of objects.
any ideas?
1 Answer
Steven Parker
243,656 Points
There are a few ways to do this, here's some generic hints:
- restructure the array to hold objects instead of individual strings
- keep the object count in a separate variable, and increment it anytime you add a new object
- add a special method to the array to compute the number of objects and use that instead of the length property
Be sure to post your code if you need more specific advice.