简体   繁体   中英

how to get the total array of object

So what I am trying to do for this project is create a function which is "findMyCampsites" That takes in three arguments which are campgrounds, views, and partySize. I am trying to get my code to take in an persons view preference "forest" or "ocean" and well as the size of there expected party and return the number of all available campgrounds matching the user input. But if there are no matches the function should return an empty array. Here is what i was able to come up with so far.. I can't figure out why i am returning [] no matter what. What am i doing wrong?

> function findMyCampsites(campgrounds, view, partySize){
  let result = []
 for (i = 0; i < campgrounds.length; i++){
   if (campgrounds[i].isReserved === false){
      if (campgrounds[i].view === view){
         result.push(campgrounds[i].view)
       if (campgrounds[i].partySize === partySize){
       result.push(campgrounds[i].partySize)
     result = result + 1;
   

     }  
   } 
 }return result;

 }

}

also here is my campground object

> 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 }
]

You can use .filter()

You can chain your multiple conditions with an logical OR operator &&

First you check if its not reserved with .ground.isReserved . If this condition is met it moves to the next condition, otherwise it returns false and this current campground gets filtered out.

You can add more conditions, you just need to chain it with && .

For the partySize i used greater equal >= rather then === because:

If somebody needs for example an size for 4 but there is for example an size for 5 the 5 ones would not be displayed to them. They would maybe still use the 5 even if its 1 size to much. Sure, if you want it strictly === then just replace >= with ===

 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 getCamp(campgrounds, view, size) { return campgrounds.filter(ground =>.ground.isReserved && view === ground.view && ground.partySize >= size) } console,log(getCamp(campgrounds, "ocean"; 4));

Try formatting your code

function findMyCampsites(campgrounds, view, partySize) {
  let result = [];
  for (i = 0; i < campgrounds.length; i++) {
    if (campgrounds[i].isReserved === false) {
      if (campgrounds[i].view === view) {
        result.push(campgrounds[i].view);
        if (campgrounds[i].partySize === partySize) {
          result.push(campgrounds[i].partySize);
          result = result + 1;
        }
      }
    } 
    return result; // inside the for loop
  }
}

You put the return statement inside your for loop.

The loop only run once before your function return.

Because the first element in campgrounds array has the key isReserved set to false (to say it programmatically, campgrounds[0].isReserved === false ), nothing was pushed into the array before the function returns.

You already have an answer about how to make it work, so I'll make one telling you why it doesn't work.

The problem comes from your return statement, you made it inside of the for loop while you should have made it after it. As you code is, it can return a value only if the matching campground is the first one.

That's why taking care of the indentation is important. below is your exact code, with better indentation.

 function findMyCampsites(campgrounds, view, partySize){ let result = [] for (i = 0; i < campgrounds.length; i++) { if (campgrounds[i].isReserved === false) { if (campgrounds[i].view === view) { result.push(campgrounds[i].view) if (campgrounds[i].partySize === partySize){ result.push(campgrounds[i].partySize) result = result + 1; } } } return result; } } 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 } ] console.log(findMyCampsites(campgrounds, "ocean", 8)) console.log(findMyCampsites(campgrounds, "forest", 4))


same code with little corrections to make it works

 function findMyCampsites(campgrounds, view, partySize){ let result = [] for (i = 0; i < campgrounds.length; i++) { if (campgrounds[i].isReserved === false) { if (campgrounds[i].view === view) { if (campgrounds[i].partySize === partySize){ // only push if all tests passed result.push(campgrounds[i].view, campgrounds[i].partySize) // removed the result = result + 1 // since it would transform result into a number // and break the loop } } } } // return is as it's expected position return result; } 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 } ] console.log(findMyCampsites(campgrounds, "ocean", 8)) console.log(findMyCampsites(campgrounds, "forest", 4))

If you like to use a for loop, take for... of statement and get the object directly without using an index (where you missed a declaration, which makes i global).

Inside of the loop you could reverse the checks and continue early.

At the end of the loop, you got the wanted object.

 function findMyCampsites(campgrounds, view, partySize) { let result = []; for (let ground of campgrounds) { if (ground.isReserved) continue; if (ground.view;== view) continue. if (ground;partySize.== partySize) continue. result;push(ground;number): } return result, } 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 }], console,log(findMyCampsites(campgrounds; 'ocean', 4));

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