1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+
4
+ < head >
5
+ < meta charset ="UTF-8 ">
6
+ < meta name ="viewport " content ="width=device-width,initial-scale=1.0 ">
7
+ < title >
8
+ Array Methods
9
+ </ title >
10
+ < link rel ="stylesheet " href ="../../base.css ">
11
+ </ head >
12
+
13
+ < body >
14
+ < script >
15
+ const toppings = [ 'Mushrooms ' , 'Tomatoes' , 'Eggs' , 'Chili' , 'Lettuce' , 'Avocado' , 'Chiles' , 'Bacon' , 'Pickles' , 'Onions' , 'Cheese' ] ;
16
+ const buns = [ 'egg' , 'wonder' , 'brioche' ] ;
17
+ const meats = {
18
+ beyond : 10 ,
19
+ beef : 5 ,
20
+ pork : 7
21
+ } ;
22
+ const prices = {
23
+ hotDog : 453 ,
24
+ burger : 765 ,
25
+ sausage : 634 ,
26
+ corn : 234 ,
27
+ } ;
28
+ const orderTotals = [ 342 , 1002 , 523 , 34 , 634 , 854 , 1644 , 2222 ] ;
29
+ const feedback = [
30
+ { comment : 'Love the burgs' , rating : 4 } ,
31
+ { comment : 'Horrible Service' , rating : 2 } ,
32
+ { comment : 'Smoothies are great, liked the burger too' , rating : 5 } ,
33
+ { comment : 'Ambiance needs work' , rating : 3 } ,
34
+ { comment : 'I DONT LIKE BURGERS' , rating : 1 } ,
35
+ ] ;
36
+
37
+ // ******************************************************************
38
+ // Static Methods
39
+ // ******************************************************************
40
+
41
+ // Array.of();
42
+
43
+ // Make a function that creates a range from x to y with Array.from();
44
+ function createRange ( start , end ) {
45
+ const range = Array . from ( { length : end - start + 1 } , function ( _item , index ) {
46
+ return index + start ;
47
+ } ) ;
48
+ return range ;
49
+ }
50
+ const myRange = createRange ( 5 , 10 ) ;
51
+
52
+ // Check if the last array you created is really an array with Array.isArray();
53
+ console . log ( Array . isArray ( myRange ) ) ;
54
+
55
+ // Take the meats object and make three arrays with Object.entries(), Object.keys, Object.values()
56
+ console . log ( Object . entries ( meats ) ) ;
57
+ console . log ( Object . keys ( meats ) ) ;
58
+ console . log ( Object . values ( meats ) ) ;
59
+
60
+ Object . values ( meats ) . forEach ( quantity => {
61
+ console . log ( quantity ) ;
62
+ } ) ;
63
+
64
+ // ******************************************************************
65
+ // Instance Methods
66
+ // ******************************************************************
67
+
68
+
69
+ // Display all bun types with " or " - use join()
70
+ console . log ( buns . join ( ' or ' ) ) ;
71
+
72
+
73
+
74
+ // We have a string "hot dogs,hamburgers,sausages,corn"
75
+ // - use split() to turn it into an array
76
+ const foodString = 'hot dogs,hamburgers,sausages,corn' ;
77
+ console . log ( foodString . split ( ',' ) ) ;
78
+ console . log ( foodString . split ( '' ) ) ; // woah!
79
+
80
+ // take the last item off toppings with pop()
81
+ const poppedTopping = toppings . pop ( ) ;
82
+ console . log ( `you popped the ${ poppedTopping } !` ) ;
83
+
84
+ // add it back with push()
85
+ toppings . push ( poppedTopping ) ;
86
+ console . log ( toppings ) ;
87
+
88
+ // take the first item off toppings with shift()
89
+ const shiftedTopping = toppings . shift ( ) ;
90
+ console . log ( shiftedTopping ) ;
91
+ console . log ( toppings ) ;
92
+
93
+ // add it back in with unshift()
94
+ toppings . unshift ( shiftedTopping ) ;
95
+ console . log ( toppings ) ;
96
+
97
+
98
+
99
+ // Do the last four,but immutable (with spreads and new variables)
100
+
101
+ // take the last item off toppings
102
+ let newToppings = [
103
+ ...toppings . slice ( 0 , toppings . length - 1 )
104
+ ] ;
105
+ console . log ( newToppings ) ;
106
+
107
+ // add it back
108
+ newToppings = [ ...toppings ] ;
109
+ console . log ( newToppings ) ;
110
+
111
+ // take the first item off toppings
112
+ newToppings = [ ...toppings . slice ( 1 ) ] ;
113
+ console . log ( newToppings ) ;
114
+
115
+ // Make a copy of the toppings array with slice()
116
+ const sliceCopyToppings = toppings . slice ( 0 ) ;
117
+ console . log ( sliceCopyToppings ) ;
118
+
119
+ // Make a copy of the toppings array with a spread
120
+ const spreadCopyToppings = [ ...toppings ] ;
121
+ console . log ( spreadCopyToppings ) ;
122
+
123
+ // take out items 3 to 5 of your new toppings array with splice()
124
+ spreadCopyToppings . splice ( 3 , 5 ) ;
125
+
126
+ // find the index of Avocado with indexOf() / lastIndexOf()
127
+ const avoIndex = toppings . indexOf ( 'Avocado' ) ;
128
+
129
+ // Check if hot sauce is in the toppings with includes()
130
+ const containsHotSauce = toppings . includes ( 'Hot Sauce' ) ;
131
+
132
+ // add it if it's not
133
+ if ( ! containsHotSauce ) {
134
+ toppings . push ( 'Hot Sauce' ) ;
135
+ }
136
+
137
+ // flip those toppings around with reverse()
138
+ const reversedToppings = [ ...toppings ] . reverse ( ) ;
139
+
140
+ // Callback Methods
141
+
142
+ // find the first rating that talks about a burger with find()
143
+ function findBurgRating ( theFeedback ) {
144
+ console . log ( theFeedback ) ;
145
+ }
146
+ const burgRating = feedback . find ( findBurgRating ) ;
147
+ // const burgRating = feedback.find(review => review.comment.includes('burg'));
148
+ console . log ( burgRating ) ;
149
+
150
+ // find all ratings that are above 2 with filter()
151
+ // find all ratings that talk about a burger with filter()
152
+ // Remove the one star rating however you like!
153
+ // check if there is at least 5 of one type of meat with some()
154
+ // make sure we have at least 3 of every meat with every()
155
+ // sort the toppings alphabetically with sort()
156
+ // sort the order totals from most expensive to least with .sort()
157
+ // Sort the prices with sort()
158
+
159
+ // Looping Methods
160
+
161
+
162
+ </ script >
163
+ </ body >
164
+
165
+ </ html >
0 commit comments