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