filter array items
variable items stores an array of items that we're going
to use for filter method, so let's assume that we want to get all
the items in this list that are less
than or equal to a hundred of
price all we would need to use is the
filter method to filter out everything
that's not under a hundred.
let's just say that we have here a
variable which is going to be filtered
items we want to set that equal to items.filter which is the filter method on
the array and this filter method just
takes a single function which is going
to have one parameter of item which is
just each item inside of the array and
then we need to return a true or false
statement on whether or not we want to
include that in the new array so we can
just say return item dot price is less
than or equal to 100
this was saying all
the items that are less than 100 in
price are going to be in our new
filtered items array and to test that we
could just console.log that
filtered items array.
output
you see we get an array over here for items all of them with a price less than or equal to 100 . All you do is return true or false for each item and if it's true it's in the new array and if it's false it's not in the new array and the great thing about this filter method.
advantage of filter function
filter method doesn't change the underlying
object that you're filtering over so we
can log the items array and as you can
see this items array still has all of
our different items all seven of them
while our filtered array has our four
filtered items so it got rid of the
three expensive items over a hundred
and that's super convenient
because we don't have to worry about
changing the old array.