6
6
let months = [" January" , " February" , " March" , " April" ];
7
7
8
8
for (let i = 0 ; i < months .length ; i++ ) {
9
- console .log (months[i]);
9
+ console .log (months[i]);
10
10
}
11
11
// January
12
12
// February
@@ -37,8 +37,8 @@ names.unshift("Samarth");
37
37
console .log (names); // [ 'Samarth', 'Bob', 'David', 'Mark' ]
38
38
39
39
// Array Splice - It adds or removes values in any position of an array
40
- names .splice (2 , 0 , " Divyanshi " , " Ayushi " );
41
- console .log (names); // [ 'Samarth', 'Bob', 'Divyanshi ', 'Ayushi ', 'David', 'Mark' ]
40
+ names .splice (2 , 0 , " Shruti " , " Rishi " );
41
+ console .log (names); // [ 'Samarth', 'Bob', 'Shruti ', 'Rishi ', 'David', 'Mark' ]
42
42
43
43
names .splice (2 , 2 );
44
44
console .log (names); // [ 'Samarth', 'Bob', 'David', 'Mark' ]
@@ -48,7 +48,7 @@ const noOneLikesSam = names.slice(1);
48
48
console .log (noOneLikesSam); // [ 'Bob', 'David', 'Mark' ]
49
49
```
50
50
51
- #### forEach method
51
+ #### forEach method
52
52
53
53
``` js
54
54
const numbers = [2 , 4 , 6 , 8 ];
@@ -71,13 +71,13 @@ console.log(sum); // 20
71
71
72
72
``` js
73
73
const inventory = [
74
- { price: 7 , name: " egg" },
75
- { price: 10 , name: " lays" },
76
- { price: 12 , name: " maggie" },
74
+ { price: 7 , name: " egg" },
75
+ { price: 10 , name: " lays" },
76
+ { price: 12 , name: " maggie" },
77
77
];
78
78
79
79
// Array Map
80
- const prices = inventory .map ((item ) => console .log (item .price ));// displays only the prices
80
+ const prices = inventory .map ((item ) => console .log (item .price ));// displays only the prices
81
81
const names = inventory .map ((item ) => console .log (item .name )); // diplays only the names
82
82
```
83
83
@@ -94,21 +94,21 @@ console.log(negativeNumbers);
94
94
95
95
// Another real life example
96
96
const employeesData = [
97
- { name: " Shubham" , overtime: 5 },
98
- { name: " Samarth" , overtime: 7 },
99
- { name: " Seema" , overtime: 8 },
97
+ { name: " Shubham" , overtime: 5 },
98
+ { name: " Samarth" , overtime: 7 },
99
+ { name: " Seema" , overtime: 8 },
100
100
];
101
101
102
102
const employeesToReward = employeesData .filter (
103
- (employee ) => employee .overtime >= 7
103
+ (employee ) => employee .overtime >= 7
104
104
);
105
105
106
106
const employeeNames = employeesToReward .map ((employee ) => employee .name );
107
107
108
- employeeNames .forEach ((user )=> {
109
- console .log (` Congratulations, ${ user} ` ); // Congratulations, Samarth
110
- // Congratulations, Seema
111
- })
108
+ employeeNames .forEach ((user )=> {
109
+ console .log (` Congratulations, ${ user} ` ); // Congratulations, Samarth
110
+ // Congratulations, Seema
111
+ });
112
112
```
113
113
114
114
#### Array Find
@@ -138,9 +138,9 @@ console.log(city); // New Delhi
138
138
const movies = [" Avengers" , " Superman" , " Batman" ];
139
139
140
140
if (movies .includes (" Avengers" )) {
141
- console .log (" The movie is available on prime" );// The movie is available on prime
141
+ console .log (" The movie is available on prime" );// The movie is available on prime
142
142
} else {
143
- console .log (" The movie is not available on prime." );
143
+ console .log (" The movie is not available on prime." );
144
144
}
145
145
146
146
// Note Includes method is case sensitive
@@ -152,9 +152,9 @@ if (movies.includes("Avengers")) {
152
152
// Array sort => Alphabetically,
153
153
// doesn't sort numbers
154
154
// This sort method mutates the original array
155
- const names = [" Shubham" , " Aditya" , " Divyanshi " , " Samarth" ];
155
+ const names = [" Shubham" , " Aditya" , " Shruti " , " Samarth" ];
156
156
names .sort ();
157
- console .log (names); // [ 'Aditya', 'Divyanshi ', 'Samarth', 'Shubham' ]
157
+ console .log (names); // [ 'Aditya', 'Shruti ', 'Samarth', 'Shubham' ]
158
158
159
159
const numbers = [4 , 12 , 8 , 5 , 1 ];
160
160
0 commit comments