简体   繁体   中英

Filter data by multiple criteria

I need to filter through a data set to find data that matches given criteria within a function

For example:

Example input/output

findMyCampsites(campgrounds, 'ocean', 8) //-> [1]
findMyCampsites(campgrounds, 'forest', 4) //-> [18]
findMyCampsites(campgrounds, 'forest', 6) //-> 'Sorry, no campsites with that view are available to host your party'

This is the data set I've been given

let campgrounds = [
  { number: 1, view: 'ocean', partySize: 8, isReserved: false },
  { number: 5, view: 'ocean', partySize: 4, isReserved: false },
  { number: 12, view: 'ocean', partySize: 4, isReserved: true },
  { number: 18, view: 'forest', partySize: 4, isReserved: false },
  { number: 23, view: 'forest', partySize: 4, isReserved: true }
];

I've tried this

function findMyCampsites(campgrounds, location, groupSize) {
  var available = []
  for (var i = 0; i < campgrounds.length; i++) {
    if (campgrounds[i].isReserved == false && campgrounds[i].view == location) {
      available.push(campgrounds[i].number)
    }
  }
  return available
}

My issue is every time I add in the partySize variable whether through an else / if conditional statement or just go right ahead and add in the else statement I'm always coming up short on the output and I have no clue what I'm doing wrong.

I'm admittedly new to this so I apologize if I'm being unclear ill add screenshots to help with that screenshot:

截屏

This works fine for me. Note that you should use let instead of var .

 let campgrounds = [ { number: 1, view: 'ocean', partySize: 8, isReserved: false }, { number: 5, view: 'ocean', partySize: 4, isReserved: false }, { number: 12, view: 'ocean', partySize: 4, isReserved: true }, { number: 18, view: 'forest', partySize: 4, isReserved: false }, { number: 23, view: 'forest', partySize: 4, isReserved: true } ]; function findMyCampsites(location, groupSize) { let available = []; for (let i = 0; i < campgrounds.length; i++) { const campground = campgrounds[i]; if (campground.isReserved == false && campground.view == location && groupSize <= campground.partySize) { available.push(campground.number) } } return available; } // The following is just for the HTML form let inputs = document.querySelectorAll('input'); let submit = document.querySelector('button'); let results = document.querySelector('div#results'); submit.addEventListener('click', function() { let location = inputs[0].value; let groupSize = inputs[1].value; results.innerHTML += '<p><b>' + location + ',&nbsp;' + groupSize.toString() + '</b>' + '&nbsp;'.repeat(4) + findMyCampsites(location, groupSize) + '</p>\n'; inputs[0].value = ''; inputs[1].value = ''; });
 <input id="location" type="text" placeholder="Location"> <input id="groupSize" type="number" placeholder="Group size"> <button>Find</button> <div id="results"> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM