2
2
// Array is an object (non-primitive)
3
3
// Array are mutable which can be directly accessed and changed
4
4
5
- let marks = [ 90 , 70 , 85 , 24 ] ;
5
+ let marks = [ 90 , 70 , 85 , 24 ] ;
6
6
console . log ( marks ) ;
7
7
console . log ( marks . length ) ; //property not a method
8
8
9
- //**************************************************************************************/
9
+ //**************************************************************************************/
10
10
// Looping Over Arrays
11
11
12
12
// For Loop
13
- for ( let i = 0 ; i < marks . length ; i ++ ) {
14
- console . log ( marks [ i ] ) ;
13
+ for ( let i = 0 ; i < marks . length ; i ++ ) {
14
+ console . log ( marks [ i ] ) ;
15
15
}
16
16
17
17
// For Of Loop
18
18
for ( let i of marks ) {
19
- console . log ( i ) ;
19
+ console . log ( i ) ;
20
20
}
21
21
22
- //**************************************************************************************/
22
+ //**************************************************************************************/
23
23
// Array Methods
24
- let foodItems = [ "Potato" , "Apple" , "Strawberry" , "Tomato" ] ;
25
- let drinks = [ "Coca Cola" , "Pakola" , "7UP" , "Sprite" ] ;
24
+ let foodItems = [ "Potato" , "Apple" , "Strawberry" , "Tomato" ] ;
25
+ let drinks = [ "Coca Cola" , "Pakola" , "7UP" , "Sprite" ] ;
26
26
27
27
//Push() adds the item at the end of an original array
28
28
foodItems . push ( "Chips" , "Burgers" ) ;
29
29
console . log ( foodItems ) ;
30
30
31
31
// Pop() removes the item from the end of an original array & returns it
32
- let deleted = foodItems . pop ( ) ;
32
+ let deleted = foodItems . pop ( ) ;
33
33
console . log ( `Deleted Item ${ deleted } ` ) ;
34
34
console . log ( foodItems ) ;
35
35
@@ -43,16 +43,16 @@ console.log(foodItems.concat(drinks));
43
43
foodItems . unshift ( "Pasta" ) ;
44
44
45
45
// Shift() Removes Item from the start of an array & returns
46
- let sDel = foodItems . shift ( ) ;
46
+ let sDel = foodItems . shift ( ) ;
47
47
console . log ( sDel ) ;
48
48
49
49
// Slice() Returns a piece of the array slice(startidx, endidx)
50
- console . log ( foodItems . slice ( 1 , 3 ) ) ;
50
+ console . log ( foodItems . slice ( 1 , 3 ) ) ;
51
51
52
52
// Splice() Change original array (add, remove, replace) splice(startidx, delCount, newEl1)
53
53
// 2nd index sy start krky 3 elements delete krny hain and us 2nd index pai "jam" insert krna hai
54
54
console . log ( "\n" , foodItems ) ;
55
- foodItems . splice ( 2 , 3 , "jam" ) ;
55
+ foodItems . splice ( 2 , 3 , "jam" ) ;
56
56
console . log ( "\n" , foodItems ) ;
57
57
58
58
// Add Element
@@ -65,4 +65,94 @@ console.log("\n", foodItems);
65
65
66
66
// Replace Element
67
67
foodItems . splice ( 1 , 1 , "cookie" ) ;
68
- console . log ( "\n" , foodItems ) ;
68
+ console . log ( "\n" , foodItems ) ;
69
+
70
+ //**************************************************************************************/
71
+ //WHAT ARE HIGHER ORDER FUNCTIONS / METHODS (HOD/HOM) ????????????//
72
+ /*
73
+ HOD takes functions either as parameters or returns a function as a value
74
+ Such as: forEach is a HOD function
75
+ */
76
+
77
+ //**************************************************************************************/
78
+ //ForEach Loop in Arrays
79
+
80
+ let arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ;
81
+
82
+ //forEach will take each array element as a value here.
83
+ //forEach automatically executes the function, we do not need to do it.
84
+ arr . forEach ( function printValue ( val ) {
85
+ console . log ( val ) ;
86
+ } ) ;
87
+
88
+ let cities = [ "Lahore" , "Faisalabad" , "Karachi" , "Hyderabad" ] ;
89
+ cities . forEach ( ( val ) => {
90
+ console . log ( val . toUpperCase ( ) ) ;
91
+ } ) ;
92
+
93
+ /*FOR EACH CAN TAKE 3 PARAMATERS i.e. value, index, and array (optional)*/
94
+ cities . forEach ( ( val , idx , arr ) => {
95
+ console . log ( val . toUpperCase ( ) , idx , arr ) ;
96
+ } ) ;
97
+
98
+ //**************************************************************************************/
99
+ // MAP in Arrays
100
+ // Map and ForEach are very similar to each other. They both are utilized to access each element of an array. The only difference is map returns a new array, but forEach() does not.
101
+ // forEach() is used to perform simple calculations, while map() is used when we need a new array with some specific operation performed on it.
102
+
103
+ arr . map ( ( val ) => {
104
+ console . log ( val ) ;
105
+ } ) ;
106
+
107
+ let calSq = arr . map ( ( val ) => {
108
+ return val * val ;
109
+ } ) ;
110
+
111
+ console . log ( calSq ) ;
112
+
113
+ //**************************************************************************************/
114
+ /* FILTER METHOD
115
+ Creates a new array of values that give true for a specified condition or filter
116
+ */
117
+ let even = arr . filter ( ( val ) => {
118
+ return val % 2 == 0 ;
119
+ } ) ;
120
+ console . log ( even ) ;
121
+
122
+ let odd = arr . filter ( ( val ) => {
123
+ return val % 2 !== 0 ;
124
+ } ) ;
125
+ console . log ( odd ) ;
126
+
127
+ //**************************************************************************************/
128
+ /* REDUCE METHOD
129
+ Performs some operations and reduce the array to a single. It return that single value.
130
+
131
+ It works like this:
132
+ for suppose let arr= [1,2,3,4]
133
+ const output= arr.reduce((res, curr)=>{
134
+ return res+curr;
135
+ });
136
+
137
+ 1st iteration
138
+ res=1, curr= 2 ---> after execution res=3
139
+ 2nd iteration
140
+ res= 3, curr= 3 ---> after execution res=6
141
+ 3rd iteration
142
+ res= 6, curr= 4 ---> after execution res=10
143
+
144
+ Output will be 10
145
+ */
146
+
147
+ const output = arr . reduce ( ( res , curr ) => {
148
+ return res + curr ;
149
+ } ) ;
150
+
151
+ console . log ( output ) ;
152
+
153
+ // Find the largest element in the array
154
+ const largest = arr . reduce ( ( prev , curr ) => {
155
+ return prev > curr ? prev : curr ;
156
+ } ) ;
157
+
158
+ console . log ( largest ) ;
0 commit comments