|
25 | 25 | ];
|
26 | 26 |
|
27 | 27 | // Some and Every Checks
|
| 28 | + const currentYear = (new Date()).getFullYear(); |
| 29 | + const ageLimit = 19; |
| 30 | + |
28 | 31 | // Array.prototype.some() // is at least one person 19 or older?
|
29 |
| - // Array.prototype.every() // is everyone 19 or older? |
| 32 | + const isSome = people.some(p => currentYear - p.year >= ageLimit); |
| 33 | + console.log("It's " + isSome + " that some people are " + ageLimit + " or older."); |
| 34 | + |
| 35 | + // Array.prototype.every() // is everyone 19 or older? |
| 36 | + const isEvery = people.every(p => currentYear - p.year >= ageLimit); |
| 37 | + console.log("It's " + isEvery + " that everyone is " + ageLimit + " or older."); |
30 | 38 |
|
31 | 39 | // Array.prototype.find()
|
| 40 | + const commentId = 823423; |
| 41 | + |
32 | 42 | // Find is like filter, but instead returns just the one you are looking for
|
33 | 43 | // find the comment with the ID of 823423
|
| 44 | + const comment = comments.find(c => c.id === commentId) |
| 45 | + |
| 46 | + if (comment !== undefined){ |
| 47 | + console.log(comment); |
| 48 | + } else { |
| 49 | + console.warn("Couldn't find comment " + commentId); |
| 50 | + } |
34 | 51 |
|
35 | 52 | // Array.prototype.findIndex()
|
36 | 53 | // Find the comment with this ID
|
37 |
| - // delete the comment with the ID of 823423 |
| 54 | + const commentIndex = comments.findIndex(c => c.id === commentId); |
| 55 | + // delete the comment with the ID of 823423 |
| 56 | + |
| 57 | + // Keep the original array |
| 58 | + const newComments = [ |
| 59 | + ...comments.slice(0, commentIndex), |
| 60 | + ...comments.slice(commentIndex + 1) |
| 61 | + ]; |
| 62 | + console.table(newComments); |
38 | 63 |
|
| 64 | + // OR modify it |
| 65 | + comments.splice(commentIndex, 1); |
| 66 | + console.table(comments); |
39 | 67 | </script>
|
40 | 68 | </body>
|
41 | 69 | </html>
|
0 commit comments