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
Lyric Abbott
35,595 Pointshelp...im confused with this one to
Add the grocery_item hash to the items array inside of the grocery_list hash.
6 Answers
Chris Shaw
26,676 PointsHi Lyric,
The challenge is asking you to append the dictionary grocery_item to the grocery_list items array, i.e the below.
grocery_list['items'].push(grocery_item)
Clair Griffiths
10,158 PointsHi Chris
Thanks for your answer, it was really helpful to me! Are you able to explain to me why 'items' is within square brackets? Aren't square brackets indicative on an array? My understanding is that the array within the grocery_list variable is the value that pairs with the 'item' key, not the key itself.
Thanks in advance
Clair
Chris Shaw
26,676 PointsHi Clair,
The square brackets serve different purposes depending on the context in which you use them, for example in this code challenge we use the square brackets with a string value of items against the grocery_list variable as it's a dictionary and dictionaries offer the ability to select <key:value> pairs using the square brackets.
When used standalone however that infers an array which is what we're retrieving by targeting the items key in the dictionary.
Hope that helps.
Clair Griffiths
10,158 PointsOk, that makes so much more sense now, thank you so much for taking the time to respond!
Thanks!
viacheslavklepach
5,767 PointsThx guys! This is wrong:
grocery_list['items'] = grocery_item
> {"title"=>"Grocery List", "items"=>{"title"=>"Bread", "quantity"=>1}}
This is ok:
grocery_list['items'].push(grocery_item)
> {"title"=>"Grocery List", "items"=>[{"title"=>"Bread", "quantity"=>1}]}
andynguyen9
1,629 PointsWhen I did it in the command line, it shows:
{"title"=>"Grocery List", "items"=>[]}
and then when I put grocey_list on the command line, it shows the same.
Why is that? Thank you.
Muhammad sharifi
4,455 Pointsthanks
Nasser Sanou
6,899 Pointsgrocery_list['items'].push(grocery_item)
andynguyen9
1,629 PointsI read the response, reread and didn't comprehend at all. :-( I know the formula for push is: hash1.push(hash2) but why grocery_list['items'].push(grocery_item) ??
Something wrong with me?