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

SOLVED: 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
Steven Parker
243,318 Points

I'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');

Thank 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
Steven Parker
243,318 Points

Sure, 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).

I 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?)

for (var i = 0; i < films.length; i += 1) {
films[1]['date'].listname
}

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.

function count(array) {
    array_elements = array;

    array_elements.sort();

    var current = null;
    var cnt = 0;
    for (var i = 0; i < array_elements.length; i++) {
        if (array_elements[i].listname != current) {
            if (cnt > 0) {
                document.write(current + ' comes --> ' + cnt + ' times<br>');
            }
            current = array_elements[i].listname;
            cnt = 1;
        } else {
            cnt++;
        }
    }
    if (cnt > 0) {
        document.write(current + ' comes --> ' + cnt + ' times');
    }

}

count(films);
Steven Parker
Steven Parker
243,318 Points

What I was thinking of would be more like this (based on the code from my answer):

// first, get all the (unique) names:
var allnames = new Set();
for (f of films) allnames.add(f.listname);

// then, print out a separate list for each one:
for (listname of allnames)
  printList(films, listname, "film", "date", "image", "slug");