简体   繁体   English

如何获得object的总数组

[英]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.所以我要为这个项目做的是创建一个 function,它是“findMyCampsites”,它包含三个 arguments,它们是露营地、景观和派对大小。 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.但如果没有匹配,function 应该返回一个空数组。 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这里也是我的露营地 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()您可以使用.filter()

You can chain your multiple conditions with an logical OR operator &&您可以使用逻辑 OR 运算符&&链接多个条件

First you check if its not reserved with .ground.isReserved .首先,您检查它是否未使用.ground.isReserved保留。 If this condition is met it moves to the next condition, otherwise it returns false and this current campground gets filtered out.如果满足此条件,则移动到下一个条件,否则返回false并且此当前露营地被过滤掉。

You can add more conditions, you just need to chain it with && .你可以添加更多条件,你只需要用&&链接它。

For the partySize i used greater equal >= rather then === because:对于partySize ,我使用了更大的等于>=而不是===因为:

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.如果有人需要例如 4 的尺寸,但例如有 5 的尺寸,则不会向他们显示 5 个。 They would maybe still use the 5 even if its 1 size to much.他们可能仍然会使用 5,即使它的 1 尺寸太大。 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.您将 return 语句放在 for 循环中。

The loop only run once before your function return.该循环仅在您的 function 返回之前运行一次。

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.因为campgrounds数组中的第一个元素的键isReserved设置为false (以编程方式表示, campgrounds[0].isReserved === false ),所以在 function 返回之前没有将任何内容推入数组。

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.问题出在你的 return 语句中,你在 for 循环中做了它,而你应该在它之后做了它。 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).如果您喜欢使用for循环,请使用for... of语句并直接获取 object 而不使用索引(您错过了声明,这使得i全局)。

Inside of the loop you could reverse the checks and continue early.在循环内部,您可以撤销检查并尽早continue

At the end of the loop, you got the wanted object.在循环结束时,您得到了想要的 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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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