Skip to content

Instantly share code, notes, and snippets.

@ross-u
Last active November 5, 2021 09:19
Show Gist options
  • Save ross-u/8cbb5d9c901e81ed517f041c5ce19782 to your computer and use it in GitHub Desktop.
Save ross-u/8cbb5d9c901e81ed517f041c5ce19782 to your computer and use it in GitHub Desktop.
M1 - JS | Array methods - filter()

JS | Array methods - filter()


Filter the listings - Exercise

img

Introduction

Imagine that we are working for Airbnb as developers, and we need to add a filter feature when someone is looking for a home. Imagine somebody starts their search for places in Barcelona, so we bring all the rooms available there.

But it´s summer, and the customers who are searching for rooms want the place to have a pool. Our incredible platform will include a filter feature, so we have to work on it.

Giving the following arrays of objects, let's filter just the one with a pool.


Task 1

Filter the places and only get the ones with the pool available.


Task 2

Using the filter method again, create an array named budgetPlaces which contains only Private Rooms cheaper than 170 $.


Go ahead to Repl.it, using the code below as your starting point.

const places = [
{
title: "Awesome Suite 20' away from la Rambla",
price: 200,
type: "Private Room",
pool: true,
garage: false
},
{
title: "Private apartment",
price: 190,
type: "Entire Place",
pool: true,
garage: true
},
{
title: "Apartment with awesome views",
price: 400,
type: "Entire Place",
pool: false,
garage: false
},
{
title: "Apartment in la Rambla",
price: 150,
type: "Private Room",
pool: false,
garage: true
},
{
title: "Comfortable place in Barcelona´s center",
price: 390,
type: "Entire place",
pool: true,
garage: true
},
{
title: "Room near Sagrada Familia",
price: 170,
type: "Private Room",
pool: false,
garage: false
},
{
title: "Great house next to Camp Nou",
price: 140,
type: "Entire place",
pool: true,
garage: true
},
{
title: "New apartment with 2 beds",
price: 2000,
type: "Entire place",
pool: false,
garage: true
},
{
title: "Awesome Suite",
price: 230,
type: "Private Room",
pool: false,
garage: false
},
{
title: "Apartment 10' from la Rambla",
price: 930,
type: "Entire place",
pool: true,
garage: true
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment