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