|
1 | 1 | <!DOCTYPE html> |
2 | 2 | <html lang="en"> |
3 | | -<head> |
4 | | - <meta charset="UTF-8"> |
5 | | - <title>Array Cardio 💪💪</title> |
6 | | -</head> |
7 | | -<body> |
8 | | - <p><em>Psst: have a look at the JavaScript Console</em> 💁</p> |
9 | | - <script> |
10 | | - // ## Array Cardio Day 2 |
11 | | - |
12 | | - const people = [ |
13 | | - { name: 'Wes', year: 1988 }, |
14 | | - { name: 'Kait', year: 1986 }, |
15 | | - { name: 'Irv', year: 1970 }, |
16 | | - { name: 'Lux', year: 2015 } |
17 | | - ]; |
18 | | - |
19 | | - const comments = [ |
20 | | - { text: 'Love this!', id: 523423 }, |
21 | | - { text: 'Super good', id: 823423 }, |
22 | | - { text: 'You are the best', id: 2039842 }, |
23 | | - { text: 'Ramen is my fav food ever', id: 123523 }, |
24 | | - { text: 'Nice Nice Nice!', id: 542328 } |
25 | | - ]; |
26 | | - |
27 | | - // Some and Every Checks |
28 | | - // Array.prototype.some() // is at least one person 19 or older? |
29 | | - // Array.prototype.every() // is everyone 19 or older? |
30 | | - |
31 | | - // Array.prototype.find() |
32 | | - // Find is like filter, but instead returns just the one you are looking for |
33 | | - // find the comment with the ID of 823423 |
34 | | - |
35 | | - // Array.prototype.findIndex() |
36 | | - // Find the comment with this ID |
37 | | - // delete the comment with the ID of 823423 |
38 | | - |
39 | | - </script> |
40 | | -</body> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <title>Array Cardio 💪💪</title> |
| 6 | + </head> |
| 7 | + <body> |
| 8 | + <p><em>Psst: have a look at the JavaScript Console</em> 💁</p> |
| 9 | + <script> |
| 10 | + // ## Array Cardio Day 2 |
| 11 | + |
| 12 | + const people = [ |
| 13 | + { name: "Wes", year: 1988 }, |
| 14 | + { name: "Kait", year: 1986 }, |
| 15 | + { name: "Irv", year: 1970 }, |
| 16 | + { name: "Lux", year: 2015 }, |
| 17 | + ]; |
| 18 | + |
| 19 | + const comments = [ |
| 20 | + { text: "Love this!", id: 523423 }, |
| 21 | + { text: "Super good", id: 823423 }, |
| 22 | + { text: "You are the best", id: 2039842 }, |
| 23 | + { text: "Ramen is my fav food ever", id: 123523 }, |
| 24 | + { text: "Nice Nice Nice!", id: 542328 }, |
| 25 | + ]; |
| 26 | + |
| 27 | + // Some and Every Checks |
| 28 | + // Array.prototype.some() // is at least one person 19 or older? |
| 29 | + /* const isAdult = people.some(function() { |
| 30 | + const currentYear= (new Date()).getFullYear() |
| 31 | + if(person.year - person.year >= 19) { |
| 32 | + return true; |
| 33 | + } |
| 34 | + }) */ |
| 35 | + |
| 36 | + const isAdult = people.some((person) => { |
| 37 | + const currentYear = new Date().getFullYear(); |
| 38 | + return currentYear - person.year >= 19; |
| 39 | + }); |
| 40 | + |
| 41 | + console.log({ isAdult }); |
| 42 | + // Array.prototype.every() // is everyone 19 or older? |
| 43 | + |
| 44 | + const allAdults = people.every((person) => { |
| 45 | + const currentYear = new Date().getFullYear(); |
| 46 | + return currentYear - person.year >= 19; |
| 47 | + }); |
| 48 | + console.log(allAdults); |
| 49 | + // Array.prototype.find() |
| 50 | + // Find is like filter, but instead returns just the one you are looking for |
| 51 | + // find the comment with the ID of 823423 |
| 52 | + |
| 53 | + /* |
| 54 | + const comment = comments.find(function (comment) { |
| 55 | + if (comment.id === 823423) { |
| 56 | + return true; |
| 57 | + } |
| 58 | + }); |
| 59 | + console.log(comment); |
| 60 | + */ |
| 61 | + |
| 62 | + const comment = comments.find((comment) => comment.id === 823423); |
| 63 | + console.log(comment); |
| 64 | + |
| 65 | + // Array.prototype.findIndex() |
| 66 | + // Find the comment with this ID |
| 67 | + // delete the comment with the ID of 823423 |
| 68 | + const index = comments.findIndex((comment) => comment.id === 823423); |
| 69 | + console.log(index); |
| 70 | + //comments.splice(index, 1); |
| 71 | + |
| 72 | + const newComments = [ |
| 73 | + ...comments.slice(0, index), |
| 74 | + ...comments.slice(index + 1), |
| 75 | + ]; |
| 76 | + </script> |
| 77 | + </body> |
41 | 78 | </html> |
0 commit comments