|
26 | 26 |
|
27 | 27 | // Some and Every Checks
|
28 | 28 | // Array.prototype.some() // is at least one person 19 or older?
|
| 29 | + console.log('Is at least one person 19 or older?'); |
| 30 | + const older = people.some(function(person){ |
| 31 | + const currentYear = (new Date()).getFullYear(); |
| 32 | + if (currentYear - person.year >= 19) { |
| 33 | + return true; |
| 34 | + } |
| 35 | + }); |
| 36 | + console.log({older}); |
| 37 | + |
29 | 38 | // Array.prototype.every() // is everyone 19 or older?
|
| 39 | + console.log('Is everyone 19 or older?'); |
| 40 | + const everyoneOlder = people.every(function(person){ |
| 41 | + const currentYear = (new Date()).getFullYear(); |
| 42 | + if (currentYear - person.year >= 19) { |
| 43 | + return true; |
| 44 | + } |
| 45 | + }); |
| 46 | + console.log({everyoneOlder}); |
30 | 47 |
|
31 | 48 | // Array.prototype.find()
|
32 | 49 | // Find is like filter, but instead returns just the one you are looking for
|
33 | 50 | // find the comment with the ID of 823423
|
| 51 | + console.log('Find the comment with the ID 823423'); |
| 52 | + const find = comments.find(function(people){ |
| 53 | + if(people.id == 823423){ |
| 54 | + return true; |
| 55 | + } |
| 56 | + }); |
| 57 | + console.log(find); |
34 | 58 |
|
35 | 59 | // Array.prototype.findIndex()
|
36 | 60 | // Find the comment with this ID
|
37 | 61 | // delete the comment with the ID of 823423
|
| 62 | + console.log('Find the comment with this ID'); |
| 63 | + console.log('Delete the comment with the ID of 823423'); |
| 64 | + const findIndex = comments.findIndex(function(people){ |
| 65 | + if(people.id == 823423){ |
| 66 | + return true; |
| 67 | + } |
| 68 | + }); |
| 69 | + console.log(findIndex); |
| 70 | + const newComments = [ |
| 71 | + ...comments.slice(0, findIndex), |
| 72 | + ...comments.slice(findIndex + 1) |
| 73 | + ]; |
| 74 | + |
| 75 | + |
38 | 76 |
|
39 | 77 | </script>
|
40 | 78 | </body>
|
|
0 commit comments