|
| 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 | + |
| 17 | + const buns = ['egg', 'wonder', 'brioche']; |
| 18 | + |
| 19 | + const meats = { |
| 20 | + beyond: 10, |
| 21 | + beef: 5, |
| 22 | + pork: 7 |
| 23 | + }; |
| 24 | + |
| 25 | + const prices = { |
| 26 | + hotDog: 453, |
| 27 | + burger: 765, |
| 28 | + sausage: 634, |
| 29 | + corn: 234, |
| 30 | + }; |
| 31 | + |
| 32 | + const orderTotals = [342, 1002, 523, 34, 634, 854, 1644, 2222]; |
| 33 | + |
| 34 | + const feedback = [ |
| 35 | + { comment: 'Love the burgs', rating: 4 }, |
| 36 | + { comment: 'Horrible Service', rating: 2 }, |
| 37 | + { comment: 'Smoothies are great, liked the burger too', rating: 5 }, |
| 38 | + { comment: 'Ambiance needs work', rating: 3 }, |
| 39 | + { comment: 'I DONT LIKE BURGERS', rating: 1 }, |
| 40 | + ]; |
| 41 | + |
| 42 | + /* |
| 43 | + Static Methods |
| 44 | + */ |
| 45 | + |
| 46 | + // Array.of(); |
| 47 | + |
| 48 | + // Make a function that creates a range from x to y with Array.from(); |
| 49 | + |
| 50 | + function createRange(start, end) { |
| 51 | + const range = Array.from({ length: end - start + 1 }, function (item, index) { |
| 52 | + return index + start; |
| 53 | + }); |
| 54 | + return range; |
| 55 | + } |
| 56 | + |
| 57 | + const myRange = createRange(3, 7); |
| 58 | + |
| 59 | + // Check if the last array you created is really an array with Array.isArray(); |
| 60 | + console.log(Array.isArray(myRange)); |
| 61 | + |
| 62 | + // Take the meats object and make three arrays with Object.entries(), Object.keys, Object.values() |
| 63 | + |
| 64 | + console.log(Object.entries(meats)); |
| 65 | + console.log(Object.keys(meats)); |
| 66 | + console.log(Object.values(meats)); |
| 67 | + |
| 68 | + Object.entries(meats).forEach(([meat, qty]) => { |
| 69 | + // const key = entry[0]; |
| 70 | + // const value = entry[1]; |
| 71 | + // const [key, value] = entry; |
| 72 | + console.log(meat, qty); |
| 73 | + }); |
| 74 | + /* |
| 75 | + Instance Methods |
| 76 | + */ |
| 77 | + |
| 78 | + // Display all bun types with " or " - use join() |
| 79 | + console.log(buns.join(' or ')); |
| 80 | + |
| 81 | + // We have a string "hot dogs,hamburgers,sausages,corn" - use split() to turn it into an array |
| 82 | + const foodString = "hot dogs,hamburgers,sausages,corn"; |
| 83 | + console.log(foodString.split(',')); |
| 84 | + |
| 85 | + // take the last item off toppings with pop() |
| 86 | + const lastItem = toppings.pop(); |
| 87 | + console.log(lastItem); |
| 88 | + // add it back with push() |
| 89 | + toppings.push(lastItem); |
| 90 | + console.log(toppings); |
| 91 | + // take the first item off toppings with shift() |
| 92 | + const firstItem = toppings.shift(); |
| 93 | + console.log(firstItem); |
| 94 | + // add it back in with unshift() |
| 95 | + toppings.unshift(firstItem); |
| 96 | + // Do the last four,but immutable (with spreads and new variables) |
| 97 | + let newToppings = toppings.slice(0, toppings.length - 1); |
| 98 | + newtoppings = [...newToppings, toppings[toppings.length - 1]]; |
| 99 | + console.log(newtoppings); |
| 100 | + |
| 101 | + |
| 102 | + // Make a copy of the toppings array with slice() |
| 103 | + const toppingsCopy = toppings.slice(0); |
| 104 | + toppings[0] = 'Mushy Boi'; |
| 105 | + console.log(toppings); |
| 106 | + console.log(toppingsCopy); |
| 107 | + // Make a copy of the toppings array with a spread |
| 108 | + const toppingsCopy2 = [...toppings]; |
| 109 | + // take out items 3 to 5 of your new toppings array with splice() |
| 110 | + toppingsCopy.splice(3, 3); |
| 111 | + console.log(toppingsCopy); |
| 112 | + // find the index of Avocado with indexOf() / lastIndexOf() |
| 113 | + const avoIndex = toppings.indexOf('Avocado'); |
| 114 | + console.log(avoIndex); |
| 115 | + |
| 116 | + // Check if hot sauce is in the toppings with includes() |
| 117 | + const isInToppings = toppings.includes('Hot Sauce'); |
| 118 | + console.log(isInToppings); |
| 119 | + // add it if it's not |
| 120 | + if (!isInToppings) { |
| 121 | + toppings.push('Hot Sauce'); |
| 122 | + } |
| 123 | + console.log(toppings); |
| 124 | + // flip those toppings around with reverse() |
| 125 | + // toppings.reverse(); |
| 126 | + const toppingsReversed = [...toppings].reverse(); |
| 127 | + console.log(toppingsReversed); |
| 128 | + |
| 129 | + /* |
| 130 | + Callback Methods |
| 131 | + */ |
| 132 | + |
| 133 | + // find the first rating that talks about a burger with find() |
| 134 | + |
| 135 | + // function findBurgRating(singleFeedback) { |
| 136 | + // return singleFeedback.comment.includes('burg'); |
| 137 | + // } |
| 138 | + |
| 139 | + // const findBurgRating = function (singleFeedback) { |
| 140 | + // return singleFeedback.comment.includes('burg'); |
| 141 | + // } |
| 142 | + |
| 143 | + // const findBurgRating = (singleFeedback) => singleFeedback.comment.includes('burg'); |
| 144 | + |
| 145 | + // const util = { |
| 146 | + // findBurgRating: function (singleFeedback) { |
| 147 | + // return singleFeedback.comment.includes('burg'); |
| 148 | + // } |
| 149 | + // } |
| 150 | + // const findSmootieRating = (singleFeedback) => singleFeedback.comment.includes('Smoothie'); |
| 151 | + |
| 152 | + function findByWord(word) { |
| 153 | + return function (singleFeedback) { |
| 154 | + return singleFeedback.comment.includes(word); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + const burgRating = feedback.find(findByWord('burg')); |
| 159 | + const smoothieRating = feedback.find(findByWord('Smoothie')); |
| 160 | + |
| 161 | + console.log(burgRating); |
| 162 | + // find all ratings that are above 2 with filter() |
| 163 | + function filterByMinRating(minRating) { |
| 164 | + return function (singleFeedback) { |
| 165 | + return singleFeedback.rating > minRating |
| 166 | + } |
| 167 | + } |
| 168 | + const goodReviews = feedback.filter(filterByMinRating(4)); |
| 169 | + console.table(goodReviews); |
| 170 | + // find all ratings that talk about a burger with filter() |
| 171 | + const burgRatings = feedback.filter(findByWord('burg')); |
| 172 | + console.table(burgRatings) |
| 173 | + |
| 174 | + // Remove the one star rating however you like! |
| 175 | + const legitRatings = feedback.filter(single => single.rating !== 1); |
| 176 | + console.table(legitRatings); |
| 177 | + |
| 178 | + // check if there is at least 5 of one type of meat with some() |
| 179 | + const isThereEnoughOfAtLeastOneMeat = Object.values(meats).some(meatValue => meatValue >= 5); |
| 180 | + console.log(isThereEnoughOfAtLeastOneMeat); |
| 181 | + // make sure we have at least 3 of every meat with every() |
| 182 | + const isThereEnoughOfEveryMeat = Object.values(meats).every(meatValue => meatValue >= 3); |
| 183 | + console.log(isThereEnoughOfEveryMeat); |
| 184 | + // sort the toppings alphabetically with sort() |
| 185 | + const numbers = [1, 2, 100, 3, 200, 400, 155]; |
| 186 | + const numbersSorted = numbers.sort((firstItem, secondItem) => firstItem - secondItem); |
| 187 | + console.log(numbersSorted); |
| 188 | + console.log(toppings.sort()); |
| 189 | + // sort the order totals from most expensive to least with .sort() |
| 190 | + function numberSort(a, b) { |
| 191 | + return b - a; |
| 192 | + } |
| 193 | + console.log(orderTotals.sort(numberSort)); |
| 194 | + // Sort the prices with sort() |
| 195 | + |
| 196 | + const productsSortedByPrice = Object.entries(prices).sort(function (a, b) { |
| 197 | + const aPrice = a[1]; |
| 198 | + const bPrice = b[1]; |
| 199 | + return aPrice - bPrice; |
| 200 | + }); |
| 201 | + console.table(Object.fromEntries(productsSortedByPrice)); |
| 202 | + |
| 203 | + |
| 204 | + /* |
| 205 | + Looping Methods (next) |
| 206 | + */ |
| 207 | + |
| 208 | + </script> |
| 209 | +</body> |
| 210 | + |
| 211 | +</html> |
0 commit comments