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