[英]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
:只需为pageSize
和isReserved
添加另一个条件:
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.filter 、 Array.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.