Loop through 2 select options and show API best match

Viewed 84

I want to show the best results (of "plants") from my 2 select options: "sunlight" and "water". API console.log is in the code: https://jsfiddle.net/Luckzzz/1p8bk0g5/

I want to show plants based on the combination of these 2 select options..

const url = 'https://front-br-challenges.web.app/api/v2/green-thumb/?sun=high&water=regularly&pets=false';


let sunlight = $('#sunlight');
sunlight.empty();
sunlight.append('<option selected="true" disabled>Choose sunlight</option>');
sunlight.prop('selectedIndex', 0);

let water = $('#water');
water.empty();
water.append('<option selected="true" disabled>Choose water amount</option>');
water.prop('selectedIndex', 0);


$.getJSON(url, function (data) {

    console.log(data);
  
  $.each(data, function (key, entry) {
  
    // Populate SUNLIGHT dropdown:

    sunlight.append($('<option></option>')
        .attr('value', entry.abbreviation)
      .text(entry.sun));
      
    // Populate WATER dropdown:

    water.append($('<option></option>')
        .attr('value', entry.abbreviation)
      .text(entry.water));
  })
});
1 Answers

Not sure if I understood your question correctly. You can check this fiddle.

const url = 'https://front-br-challenges.web.app/api/v2/green-thumb/?sun=high&water=regularly&pets=false';

let selectedSun, selectedWater, globalData = [];

let sunlight = $('#sunlight');
sunlight.empty();
sunlight.append('<option selected="true" disabled>Choose sunlight</option>');
sunlight.prop('selectedIndex', 0);

let water = $('#water');
water.empty();
water.append('<option selected="true" disabled>Choose water</option>');
water.prop('selectedIndex', 0);

sunlight.on('change', function() {
  selectedSun = this.value;
  showImages();
});

water.on('change', function() {
  selectedWater = this.value;
  showImages();
});

const showImages = () => {
    $("#items").empty();
  globalData.forEach(item => {
  if (!selectedWater || (selectedWater && item.water === selectedWater) && 
  (!selectedSun || (selectedSun && item.sun === selectedSun))) {
      let img = document.createElement('img');
      img.src = item.url;
      $("#items").append(img);
    }
  })

}

$.getJSON(url, function (data) {

    console.log(data);
    globalData = data;
    
    const sunlights = data.reduce((acc, curr) => {
        if (!acc.includes(curr.sun)) acc.push(curr.sun)
      return acc;
    }, []);
    
    const waterTypes = data.reduce((acc, curr) => {
        if (!acc.includes(curr.water)) acc.push(curr.water)
      return acc;
    }, []);
  
    $.each(sunlights, function (key, entry) {

      sunlight.append($('<option></option>')
          .attr('value', entry)
        .text(entry));

    })
  
    $.each(waterTypes, function (key, entry) {
  
      water.append($('<option></option>')
        .attr('value', entry)
      .text(entry));

  })
  

});

In short, I:

  • reduced those two lists, so you don't have duplicated
  • added method to show images, when one of selects changes
Related