File tree Expand file tree Collapse file tree 3 files changed +68
-0
lines changed Expand file tree Collapse file tree 3 files changed +68
-0
lines changed Original file line number Diff line number Diff line change
1
+ filter
2
+ // does return a new array
3
+ // can manipulate the size of new array
4
+ // returns based on condition
5
+
6
+ const people = [
7
+ { name : 'bob' , age : 20 , position : 'developer' } ,
8
+ { name : 'peter' , age : 25 , position : 'designer' } ,
9
+ { name : 'susy' , age : 30 , position : 'the boss' } ,
10
+ { name : 'anna' , age : 35 , position : 'the boss' } ,
11
+ ] ;
12
+
13
+ const youngPeople = people . filter ( function ( person ) {
14
+ return person . age <= 25 ;
15
+ } ) ;
16
+
17
+ const developers = people . filter ( function ( person ) {
18
+ return person . position === 'senior developer' ;
19
+ } ) ;
20
+
21
+ console . log ( developers ) ;
Original file line number Diff line number Diff line change
1
+ // forEach
2
+ // does not return new array
3
+
4
+ const people = [
5
+ { name : 'bob' , age : 20 , position : 'developer' } ,
6
+ { name : 'peter' , age : 25 , position : 'designer' } ,
7
+ { name : 'susy' , age : 30 , position : 'the boss' } ,
8
+ ] ;
9
+
10
+ function showPerson ( person ) {
11
+ console . log ( person . position . toUpperCase ( ) ) ;
12
+ }
13
+
14
+ // people.forEach(showPerson);
15
+
16
+ people . forEach ( function ( item ) {
17
+ console . log ( item . position . toUpperCase ( ) ) ;
18
+ } ) ;
Original file line number Diff line number Diff line change
1
+ // map
2
+ // does return a new array
3
+ // does not change size of original array
4
+ // uses values from original array when making new one
5
+
6
+ const people = [
7
+ { name : 'bob' , age : 20 , position : 'developer' } ,
8
+ { name : 'peter' , age : 25 , position : 'designer' } ,
9
+ { name : 'susy' , age : 30 , position : 'the boss' } ,
10
+ { name : 'anna' , age : 35 , position : 'the boss' } ,
11
+ ] ;
12
+
13
+ const ages = people . map ( function ( person ) {
14
+ return person . age + 20 ;
15
+ } ) ;
16
+ const newPeople = people . map ( function ( person ) {
17
+ return {
18
+ firstName : person . name . toUpperCase ( ) ,
19
+ oldAge : person . age + 20 ,
20
+ } ;
21
+ } ) ;
22
+
23
+ const names = people . map ( function ( person ) {
24
+ return `<h1>${ person . name } </h1>` ;
25
+ } ) ;
26
+
27
+ document . body . innerHTML = names . join ( '' ) ;
28
+
29
+ console . log ( names ) ;
You can’t perform that action at this time.
0 commit comments