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