Map, Filter & Reduce EXPLAINED in JavaScript - It's EASY!

dcode
10 Aug 202111:23

Summary

TLDRIn this informative video, Dom introduces three essential JavaScript array methods: map, filter, and reduce. He demonstrates how map creates a new array by transforming each element, filter selectively includes elements based on a condition, and reduce consolidates the array into a single value. Using an array of person objects, Dom illustrates the practical applications of these methods, showing how they can be chained for complex operations and emphasizing their utility in web development.

Takeaways

  • 🗺️ The video introduces three essential JavaScript array methods: map, filter, and reduce.
  • 🔄 The map method transforms each item in an array into a new one based on the existing item, maintaining the array's original length.
  • 🚫 The filter method allows for the creation of a new array with items that meet specific criteria, likely resulting in a shorter array.
  • 🔢 The reduce method condenses an array into a single value, which is not an array but could be a number, string, or other data type.
  • 👤 The examples in the video use an array of person objects with properties for name, age, and occupation.
  • 📝 The map method is demonstrated by creating a new array containing only the names of the people from the original array.
  • 🎯 The filter method is shown by filtering out individuals under the age of 30, resulting in an array of people over 30.
  • 🔗 The video explains that the map and filter methods can be chained together to perform multiple transformations and filtering in sequence.
  • 🧩 The reduce method is used to calculate the total age of all people in the array, demonstrating its capability to aggregate data.
  • 📊 The reduce method's parameters include an accumulator (total) and the current array item (person), with the ability to set an initial value for the accumulator.
  • 🌐 The video encourages viewers to subscribe for more web development tutorials and projects.

Q & A

  • What are the three JavaScript methods discussed in the script?

    -The three JavaScript methods discussed in the script are map, filter, and reduce.

  • What does the map method do?

    -The map method takes an array of items and converts each item into a new one based on the existing one, resulting in an array of the same length as the original.

  • How does the filter method differ from the map method?

    -The filter method takes an array and removes items that do not meet a specified criteria, likely resulting in an array with a length shorter than the original.

  • What is the primary purpose of the reduce method?

    -The reduce method is used to reduce an array to a single value, which is typically not an array but could be a number, string, or any other data type.

  • What is the purpose of the conversion function in the map method?

    -The conversion function in the map method applies to every item in the array, allowing you to transform each item into a new form, such as extracting only the name from a person object.

  • Can you provide an example of how the filter method is used in the script?

    -In the script, the filter method is used to create an array of people over the age of 30, excluding those who are younger.

  • What is the significance of chaining methods like map and filter together?

    -Chaining methods allows for more complex data manipulations in a concise manner. For example, after filtering people over 30, you can chain a map method to extract their names.

  • How does the reduce method calculate the total age of all people in an array?

    -The reduce method iteratively adds the age of each person to a running total, starting with an initial value (in this case, 0), and ultimately returns the sum of all ages.

  • What is the initial value set for the reduce method in the script?

    -The initial value set for the reduce method in the script is 0, which is the starting point for the cumulative total of ages.

  • Can the reduce method return types other than numbers?

    -Yes, the reduce method is not limited to numbers and can return any data type, such as arrays, strings, or booleans, depending on the logic implemented in the function.

Outlines

00:00

📚 Introduction to JavaScript Array Methods

In this video, Dom introduces viewers to three essential JavaScript array methods: map, filter, and reduce. He explains that 'map' is used to transform each item in an array into a new one, 'filter' to remove items that don't meet certain criteria, and 'reduce' to condense an array into a single value. Dom uses a consistent array of person objects to demonstrate each method, emphasizing the differences between them and encouraging new viewers to subscribe for more web development content.

05:01

🔍 Demonstrating the Map and Filter Methods

Dom proceeds with a practical demonstration of the 'map' method by transforming an array of person objects into an array of names. He then uses the 'filter' method to create a new array that only includes people over the age of 30, excluding those who do not meet this criterion. Dom also illustrates the concept of method chaining by combining 'filter' and 'map' to extract names of people over 30, showcasing how these methods can be sequentially applied for more complex transformations.

10:01

🔢 Utilizing the Reduce Method for Aggregation

The video concludes with an explanation of the 'reduce' method, which Dom describes as one of the more complex but powerful array methods. He uses 'reduce' to calculate the total age of all individuals in the array, starting with an initial value of 0 and cumulatively adding each person's age. Dom clarifies that 'reduce' is not limited to numerical values and can be used to aggregate various data types. He invites viewers to like and subscribe for more tutorials and ends the video with a promise of more content in the future.

Mindmap

Keywords

💡JavaScript

JavaScript is a high-level, interpreted programming language commonly used for enhancing web pages with interactive elements. In the context of the video, JavaScript is the programming language where the methods 'map', 'filter', and 'reduce' are being demonstrated and discussed. It's the core technology that the video's theme revolves around.

💡map

The 'map' method in JavaScript is used to create a new array by transforming each item in the original array based on a function provided. It is a key concept in the video, as it's one of the three methods the presenter, Dom, is focusing on. The script uses 'map' to create an array of names from an array of person objects.

💡filter

The 'filter' method in JavaScript is used to create a new array with all elements that pass a test implemented by the provided function. It's one of the core methods discussed in the video, where Dom uses it to filter out people who are not over the age of 30, demonstrating its use in creating a new array based on a condition.

💡reduce

The 'reduce' method in JavaScript is used to reduce the array to a single value. It's the most complex of the three methods discussed in the video and is used to calculate the total age of all people in the array. The method is powerful for performing operations that aggregate data into a single result.

💡Array

An array in JavaScript is a global object that is used in the script to hold collections of values. The video's examples are based on an array of person objects, and the methods 'map', 'filter', and 'reduce' are applied to this array to demonstrate their functionality.

💡Function

In JavaScript, a function is a block of code designed to perform a particular task. In the video, functions are used as parameters for the 'map', 'filter', and 'reduce' methods to define the behavior of these operations on the array elements.

💡Person Object

A person object in the script represents an individual with properties like name, age, and occupation. It's a data structure used in the examples to demonstrate how the 'map', 'filter', and 'reduce' methods work with arrays of objects.

💡Shorthand Arrow Syntax

Shorthand arrow syntax in JavaScript is a concise way to write function expressions. In the script, Dom uses this syntax when defining the functions for the 'map' and 'filter' methods, making the code more readable and succinct.

💡Chaining Methods

Chaining methods in JavaScript allows you to use multiple methods on the result of the previous method. In the video, Dom demonstrates chaining by first using 'filter' to get people over 30 and then 'map' to get their names, showing how you can combine operations for more complex data manipulation.

💡Condition

A condition in programming is a boolean expression that evaluates to true or false. In the context of the 'filter' method in the video, a condition is used to test each item in the array to determine if it should be included in the resulting array.

💡Total Age

In the video, 'total age' refers to the cumulative age of all people in the array. Dom uses the 'reduce' method to calculate this value, demonstrating how to aggregate data into a single numerical value.

Highlights

Introduction to three favorite JavaScript methods: map, filter, and reduce.

Map method explained for converting array items into new ones based on the existing ones, maintaining the original array length.

Filter method allows filtering out items that don't meet specific criteria, likely resulting in a shorter array length.

Reduce method is used to condense an array into a singular value, such as a number or a string.

Consistent use of an array of person objects for demonstrating each method.

Example of using map to create a new array containing only the names of the people.

Shorthand arrow syntax used in the map function for conciseness.

Explanation of how the map function applies a conversion function to every item in the array.

Demonstration of using filter to exclude people under the age of 30.

Filter method's function as a test to determine if an item should be included in the results.

Chaining map and filter methods to get names of people over 30.

The power of chaining methods for more complex data manipulation.

Introduction to the reduce method for calculating the total age of all people in the array.

Explanation of the reduce function parameters and their role in accumulating a result.

Setting an initial value for the reduce method to start the accumulation process.

Final result of the reduce method showing the total age calculated.

Flexibility of the reduce method to return various data types, not just numbers.

Conclusion and call to action for likes and subscriptions for more web development content.

Transcripts

play00:00

hey guys how's it going my name is dom

play00:02

and today i'm going to be taking you

play00:03

through my three favorite

play00:05

javascript methods map filter and reduce

play00:08

now before diving into the code i just

play00:10

want to briefly go over each one

play00:12

map lets you take an array of items and

play00:15

then convert each item

play00:16

into a new one based on the existing one

play00:20

the resulting array is the same length

play00:22

as the original

play00:23

filter allows you to take an array and

play00:25

then filter out

play00:27

any items that don't pass the criteria

play00:29

which you pass

play00:30

in the resulting array's length is most

play00:33

likely going to be

play00:34

less than the original and lastly reduce

play00:37

allows you to reduce down an array into

play00:40

a singular

play00:41

value which is typically not an array

play00:44

but instead things like a number or a

play00:46

string

play00:47

so now i want to head into this code and

play00:49

show you an example of each one of those

play00:51

methods

play00:52

being used and for all those examples

play00:54

i'll be using the exact same

play00:56

array of people so basically just an

play00:58

array of person objects

play01:00

just so it's consistent and you're able

play01:02

to better understand the difference

play01:04

between each one of those methods

play01:06

but before we get into that i just want

play01:07

to say if you're new around here

play01:09

consider subscribing

play01:10

as i've got plenty of fantastic web

play01:13

development tutorials and projects on my

play01:15

channel

play01:16

all right so we're going to be starting

play01:17

off with the map method

play01:19

okay so right here i've got my array of

play01:22

people

play01:22

so every single item inside this array

play01:26

is an object representing a single

play01:28

person so right here

play01:30

these people have a name age and

play01:33

occupation

play01:34

and like i mentioned earlier we're going

play01:35

to be using this same array

play01:37

in every single example in today's video

play01:40

now

play01:40

in the in the case of map we're going to

play01:43

be using the map

play01:44

method to give us a new array

play01:47

containing each person's name

play01:50

so only the name part of every single

play01:53

person here

play01:54

so we're going to just minimize this and

play01:57

then drop

play01:57

down and we're going to be declaring a

play01:59

new constant called

play02:01

name so this names constant is going to

play02:04

be the result

play02:05

of calling the map method we're going to

play02:08

say

play02:08

people dot map and then inside here

play02:12

we're going to pass through a function

play02:14

so you can think of this function as a

play02:16

conversion function

play02:18

so this conversion function is going to

play02:20

apply to every single item

play02:22

inside your array so we're going to take

play02:25

through all

play02:26

we're going to be accepting a person

play02:28

here now

play02:29

i'm using the shorthand arrow syntax

play02:31

array here

play02:32

so we can see basically this function

play02:34

here is going to apply

play02:36

for every single person inside the array

play02:39

of

play02:39

people so we're going to say right

play02:42

inside here

play02:43

return person dot name

play02:46

so essentially whatever value you decide

play02:50

to return

play02:51

from this function that is going to be

play02:54

what gets used in the resulting

play02:56

array um you know once the map method

play02:59

has been ran okay so now if our

play03:02

console.log

play03:03

the names here we can see in the bottom

play03:07

right

play03:07

upon saving we get right here an array

play03:11

of each individual person's name inside

play03:14

our original array

play03:16

and we can see also that the length of

play03:18

the array is the same as the original

play03:20

because of course with the map you're

play03:22

simply applying a conversion function

play03:25

to every single item inside your array

play03:28

and whatever you return

play03:29

from that function that is going to be

play03:31

what gets used

play03:33

in the resulting you know array so that

play03:35

is your

play03:36

map method now moving on to the filter

play03:39

method

play03:40

we're going to be using the same array

play03:42

here but this time we're going to be

play03:43

using

play03:44

filter to filter out

play03:47

any people that are not over the age of

play03:50

30. okay so in the resulting array we

play03:53

only expect

play03:54

dom and bruce to be returned

play03:58

okay so basically we just want to filter

play04:00

out amy because

play04:01

her age is under 30. okay so

play04:05

let's minimize the array once again and

play04:07

go down here and we're going to say

play04:08

const over 30s so basically

play04:12

of course as the name suggests it's

play04:14

going to be an array of people over

play04:16

30 years of age i'm going to say here

play04:19

people dot filter okay so

play04:23

the filter method much like map is going

play04:25

to take through a function

play04:27

this time the function is going to be

play04:28

more of a test okay so

play04:30

you're defining a test which must pass

play04:34

for the item to be returned inside your

play04:38

results array

play04:39

so inside here we're going to once again

play04:42

and grab on to the person and i'll just

play04:45

make this a little bit smaller so we're

play04:47

going to be grabbing on to

play04:48

the person here okay now

play04:51

it's going to be a simple case of

play04:53

returning true or false

play04:54

so if this function returns true then

play04:57

your person is going to exist in your

play04:59

resulting array

play05:00

if not they're going to be excluded so

play05:03

i can return here a a check i can say

play05:07

return person dot age is greater than or

play05:10

equal to

play05:11

30. so of course this expression is

play05:14

going to give me

play05:15

true for dom and bruce since of course

play05:19

the age is more than 30

play05:21

but for amy that condition is going to

play05:23

give me false

play05:24

therefore she's not going to be included

play05:27

in my results so now

play05:28

if i console.log the over 30s

play05:32

array i save this here we can see in the

play05:34

console

play05:35

we get two items we get the exact same

play05:37

objects as we did

play05:39

in the people so you know like we saw

play05:41

earlier map's gonna change this but

play05:43

with filter you know you get the exact

play05:45

same item back

play05:47

but of course we only get two items here

play05:49

dom and bruce

play05:50

as these ones are going to pass the

play05:52

condition which you pass through

play05:54

and that is your filter method it's also

play05:57

important to know that you can actually

play05:59

chain these methods onto one another

play06:02

okay so for example

play06:03

i've got my filtered over 30s here like

play06:06

i just showed you but

play06:07

i can then say right down here because

play06:10

the result

play06:11

of the filter method is going to be a

play06:13

new array

play06:14

it's still an array which means i can

play06:16

call dot

play06:18

map onto the result of this so i can say

play06:21

dot map

play06:21

i can once again take through a person

play06:24

object

play06:25

then i can just say once again returned

play06:28

person dot name so now we're actually

play06:31

getting

play06:32

uh the the name of every single person

play06:36

over the age of 30 with this whole chain

play06:38

here

play06:39

so if i save this we can see in the

play06:41

console we get dom and bruce

play06:43

instead of the previous example where we

play06:45

actually got the full object

play06:46

and that of course is due to the usage

play06:48

of map at the end here

play06:50

so you might definitely find it useful

play06:52

to chain your methods together

play06:54

like i've just shown and lastly we've

play06:57

got the reduce

play06:58

method okay so the reduce method is

play07:00

probably one of the more

play07:02

complicated ones out of the three but

play07:04

it's definitely super powerful

play07:06

okay so like i mentioned at the

play07:07

beginning the reduce method

play07:09

is used to reduce down your array

play07:12

into a singular value okay so right here

play07:15

with the people i want to be able to

play07:17

find the total

play07:19

of every single person's age so

play07:21

basically

play07:22

when i add up 35 plus 26 plus 55

play07:26

and we're going to be using reduce to

play07:28

achieve that okay

play07:30

so going down here let's make a new

play07:32

constant

play07:33

called total age okay of course

play07:36

this total age refers to the results of

play07:39

all those numbers

play07:40

added up and now we're going to use

play07:43

reduce

play07:44

so we're going to say people dot reduce

play07:47

okay then it's going to take through a

play07:50

function

play07:50

just like the previous two methods so

play07:53

this one here

play07:54

is going to take through multiple

play07:56

parameters so i'm going to pass through

play07:58

here

play07:59

total and then person okay so

play08:03

let me explain these two parameters

play08:05

right here

play08:06

so this function is going to run for

play08:08

every single

play08:09

person inside my array or every single

play08:12

item inside my array

play08:14

and that is what your person is going to

play08:16

be just like the previous two examples

play08:18

so

play08:19

person is always going to refer to the

play08:21

object down here name

play08:23

age and occupation this total here

play08:26

this essentially is going to represent

play08:30

the current value as we loop through

play08:33

every single person so we need this

play08:36

temporary

play08:37

uh parameter okay to

play08:40

you know cumulatively add up every

play08:42

single age so

play08:43

it's basically just a placeholder which

play08:45

eventually turns into

play08:46

our final results much like if you were

play08:49

to loop through this person

play08:51

or these people array manually and try

play08:53

and find the total

play08:54

you'd have a variable called total which

play08:57

you continuously add to

play08:58

that is what this one is right here so

play09:01

when the reduce method first runs and

play09:03

the first

play09:05

item here is going to be you know this

play09:07

person here

play09:08

named dom h35 when this first object

play09:12

goes through what is the total going to

play09:16

be

play09:16

well you can set a initial value

play09:19

so that is done by going right down here

play09:22

and providing a value

play09:24

to you know right after your function

play09:28

as part of the reduce method so right

play09:30

here comma

play09:31

then say 0 we're going to start counting

play09:33

at 0.

play09:34

so inside here we're going to return

play09:38

total plus person dot age

play09:42

okay so basically we're saying right

play09:43

here total

play09:45

is going to be 0 in this case in the

play09:47

first

play09:48

object so 0 plus 35

play09:52

for this first one here 35 so 0 plus 35

play09:57

the next object once the once the

play10:00

function runs again

play10:01

and we have you know our next person

play10:03

inside here

play10:04

this total is going to be whatever

play10:07

that was returned from the previous

play10:10

iteration

play10:11

so in this case here it's going to be 35

play10:14

whatever 0 plus 35 is that is going to

play10:17

be what gets passed to the total

play10:19

in the next iteration so the next time

play10:22

this runs right

play10:23

when it runs for amy it's going to be 35

play10:27

plus 26 and then as it continues to run

play10:31

as we iterate through every single

play10:32

person in the array this total is going

play10:34

to grow

play10:35

and once we're done this total is going

play10:38

to be what gets

play10:39

you know used in the return here of

play10:42

total age

play10:43

so if our console.log the total age

play10:47

result here save this we get 116 inside

play10:51

the console so

play10:53

that is what your reduce method does now

play10:56

obviously

play10:57

you're not limited to numbers here so

play11:00

commonly

play11:00

you're going to find numbers being used

play11:02

but you can actually return whatever you

play11:04

want inside here

play11:05

it's totally up to you it can be arrays

play11:08

strings

play11:09

booleans whatever you want so that is

play11:11

the reduce method

play11:12

right there and that is all for today

play11:14

guys if you liked drop a like and

play11:16

subscribe i really appreciate it and

play11:18

i'll see you guys in the next video

Rate This

5.0 / 5 (0 votes)

Связанные теги
JavaScriptMap MethodFilter MethodReduce MethodArray ManipulationWeb DevelopmentTutorialsProgrammingDOMFunctional ProgrammingEducational
Вам нужно краткое изложение на английском?