简体   繁体   English

给定露营地数组,编写一个返回字符串和匹配数字的函数

[英]Given campground array, write a function that returns a string and matching number

I've been given a campgrounds array and I need to write a function, findMyCampsites() with parameters, (campgrounds, view, partySize).我得到了一个露营地数组,我需要编写一个函数 findMyCampsites() 和参数(露营地、视图、派对大小)。 I need to filter out the ones that are unreserved, then return an array with campsite numbers for the matching campsites.我需要过滤掉那些没有保留的,然后返回一个包含匹配露营地的露营地编号的数组。

Example: findMyCampsites(campgrounds, 'ocean', 8) //-> [1]示例: findMyCampsites(campgrounds, 'ocean', 8) //-> [1]

And if the input is one that is not in the array it will return 'Sorry, no campsites with that view are available to host your party'.如果输入不在数组中,它将返回“抱歉,没有具有该视图的露营地可用于举办您的派对”。

Example: findMyCampsites(campgrounds, 'forest', 6) //-> 'Sorry, no campsites with that view are available to host your party'示例: findMyCampsites(campgrounds, 'forest', 6) //-> 'Sorry, no campsites with that view are available to host your party'

Given array:给定数组:

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

What I have so far:到目前为止我所拥有的:

function findMyCampsites(campgrounds, view, partySize) {
  let total = 0;
  for (let i = 0; i < campgrounds.length; i++) {
    if (campgrounds[i].view === view) {
      total++;
    }
    return total;
  }
}

I know I need to return the campsite's number that is available and that can fit the party size and which have their desired view, but I don't know what to do after the for loop..我知道我需要返回可用的露营地编号,该编号可以适合派对规模并具有他们想要的视图,但我不知道在 for 循环之后该怎么做..

Just add another condition for the pageSize and isReserved :只需为pageSizeisReserved添加另一个条件:

 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(campgrounds, view, partySize) { let campsites = []; for (let i = 0; i < campgrounds.length; i++) { if (campgrounds[i].view === view && campgrounds[i].partySize == partySize && !campgrounds[i].isReserved) { campsites.push(campgrounds[i].number); } } return campsites.length>0 ? campsites : 'Sorry, no campsites with that view are available to host your party'; } console.log(findMyCampsites(campgrounds, 'ocean', 8)); console.log(findMyCampsites(campgrounds, 'forest', 6));

Again, you can easily achieve this with .filter :同样,您可以使用.filter轻松实现这.filter

 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(campgrounds, view, partySize) { let campsites = campgrounds.filter(campground => (campground.view === view && campground.partySize == partySize && !campground.isReserved) ).map(e => e.number); return campsites.length>0 ? campsites : 'Sorry, no campsites with that view are available to host your party'; } console.log(findMyCampsites(campgrounds, 'ocean', 8)); console.log(findMyCampsites(campgrounds, 'forest', 6));

You can use the filter method to iterate through each element of the array and filter by different properties您可以使用 filter 方法遍历数组的每个元素并按不同的属性进行过滤

function findMyCampsites(campgrounds, view, partySize) {
   const camp = campgrounds.filter(
      camp => camp.view == view 
         && camp.partySize == partySize 
         && camp.isReserved == false)

   return camp.length >= 1 ? camp.length : "No campsites where found"
}

Here's a one-liner using Array.prototype.filter , Array.prototype.map , and assignment returns :这是使用Array.prototype.filterArray.prototype.map赋值返回的单行代码:

 let findMyCampsite = (campgrounds, view, partySize) => ( (res = campgrounds.filter(cg => ( !cg.isReserved && partySize <= cg.partySize && view === cg.view )).map(({ number }) => number)).length > 0 ? res : 'Sorry, no campsites with that view are available to host your party' ) 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(findMyCampsite(campgrounds, 'ocean', 4)); console.log(findMyCampsite(campgrounds, 'ocean', 9));

暂无
暂无

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

相关问题 给定露营地数组,编写一个返回总和和匹配输入字符串的函数 - Given campgrounds array, write a function that returns a sum and matching input string 编写一个返回给定数组中零个数的函数? - Write a function that returns the number of zeros in a given array? 给定数组,写一个 function 找到最大值并返回匹配的字符串 - Given array, write a function that finds the max and return the matching string 给定数组,编写函数以返回匹配项 - Given array, write function to return matching items 给定数组,编写一个返回订单对象的函数 - Given array, write a function that returns an order object 给定数组,写一个 function 返回数组中与该问题匹配的项目 - Given array, write a function that returns the item in the array that matches that question 在JavaScript中,给定输入字符串,创建一个函数,该函数返回包含n个2d数组的数组 - In JavaScript, given an input string, create a function that returns an array containing an n number of 2d arrays Javascript:编写一个接受数字的函数,并多次返回带有该数字的数组 - Javascript: Write a function that takes in a number, and returns an array with that number in it that many times 在Javascript中是否有一个函数返回给定字符串出现的次数? - In Javascript is there a function that returns the number of times that a given string occurs? 编写一个函数,删除给定对象上的任何属性,其值是长于给定数字的字符串,并返回该对象 - Write a function that removes any properties on the given object whose values are strings longer than the given number and returns the object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM