Reputation:
I am given an array of data for a campsite, one of the key:value pairs being isReserved: false. I need to write a function called availableCampsites() and return an array with the campsites that are unreserved.
Here is the given campground 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: (I've only been learning JS a few days, I know my code will be pretty bad)
function availableCampsites(campgrounds) {
var openCampsites = [];
var unreserved = campgrounds.isReserved;
if (unreserved === false) {
openCampsites.push(unreserved);
}
return openCampsites;
}
Upvotes: 0
Views: 242
Reputation: 50759
As you mentioned you're new to JS, I think you should stick to the basics before moving to higher-order functions such as reduce, filter, etc. You've got the right idea, you're just missing the code which will loop through your array. In your function, campgrounds
represents an array, which contains objects. You need to go through each object, and check if its isReserved
property is false, and if it is, add it (push it), into your openCampsites
array. In order to go through your array of objects, you can use a for
loop, which will allow you to "grab" each object from your array, and check its isReserved
property.
See code comments and example below for more details:
function availableCampsites(campgrounds) {
let openCampsites = [];
// Loop through each index `i` in your array
for(let i = 0; i < campgrounds.length; i++) {
let currentCampsite = campgrounds[i]; // get current camp ground at index `i` from your array
// If current campsites isReserved property is false, then...
if (currentCampsite.isReserved === false) {
openCampsites.push(currentCampsite); // add/push the currentCampsite object into your newly created array
}
}
return openCampsites;
}
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(availableCampsites(campgrounds));
Upvotes: 1
Reputation: 652
use JavaScript filter:
function availableCampsites(campgrounds) {
return campgrounds.filter(campground => campground.isReserved == false);
}
Upvotes: 2
Reputation: 10194
You can simply do it using Array.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 }
];
const result = campgrounds.filter(({ isReserved }) => isReserved === false);
console.log(result);
Upvotes: 1