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
Alison Mercer
46 PointsSOLVED: Anyone know how to select from an associative array only the sets where a key equals a specified value?
If anyone knows how to select from the array created below in javascript and output it so the list_name is displayed before each list instead of each film as it currently does and if it is possible to only select a specific list form the array then please help, thank you.
SOLVED see below.
1 Answer
Steven Parker
243,318 PointsI'm a bit confused when you say you want it "so the list_name is displayed before each list instead of each film...", did you perhaps mean "so the list_name is displayed before each list of films..."? It doesn't seem like it would be useful to display the list name instead of the film.
But you can get what I think you want with just a few changes:
// note: added new "selected" 2nd argument:
function printList( list, selected, object, secondobject, thirdobject, fourthobject ) {
var listHTML = '<h2>Your "' + selected + '" list:</h2><ol>'; // display the list name first
for (var i = 0; i < list.length; i += 1) {
if (list[i].listname == selected) // only print the items that match the "selected" list name
listHTML += '<li>' + list[i][object] + list[i][secondobject] + '<a href="' + list[i][fourthobject] + '">' + '<img src="/images/' + list[i][thirdobject] + '" width="135px" height="200px">' +'</a>' + '</li>';
}
listHTML += '</ol>';
print(listHTML);
}
// when calling the function, provide the desired list name as the new 2nd argument:
printList(films, 'Watched', 'film', 'date', 'image', 'slug');
Alison Mercer
46 PointsAlison Mercer
46 PointsThank you, Steven Parker. That is what I meant and it works great. Just one other thing, do you know if it's possible to display all the lists one after another, each with the heading "Your "(LIST NAME)" list:"? For example on an all lists page, as the number of lists is variable.
Steven Parker
243,318 PointsSteven Parker
243,318 PointsSure, you could put your call to "printList" in a loop where you pass it each of the possible list names (I assume you have them enumerated somewhere).
Alison Mercer
46 PointsAlison Mercer
46 PointsI created a loop but it displays the list multiple times based on how many films there are in the list, I think this is as I was unable to find a way to group the array by each list in PHP. Do you know if there any way to counter this in Javascript? (Perhaps by only counting the list id/name when it changes?)
I am able to count the number of times each list appears using this script if that helps you see a way you might know.
Steven Parker
243,318 PointsSteven Parker
243,318 PointsWhat I was thinking of would be more like this (based on the code from my answer):