|
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) { // this is function declaration |
| 30 | + // const currentYear = (new Date()).getFullYear() |
| 31 | + // if (person.year - person.year >= 19) { |
| 32 | + // return true |
| 33 | + // } |
| 34 | + // }) |
| 35 | + const isAdult = people.some(person => { // This is the ES6 way (arrow function) |
| 36 | + const currentYear = (new Date()).getFullYear() |
| 37 | + return currentYear - person.year >= 19 |
| 38 | + }) |
| 39 | + console.log({isAdult}) |
| 40 | + |
29 | 41 | // Array.prototype.every() // is everyone 19 or older?
|
| 42 | + const allAdult = people.every(person => { // This is the ES6 way (arrow function) |
| 43 | + const currentYear = (new Date()).getFullYear() |
| 44 | + return currentYear - person.year >= 19 |
| 45 | + }) |
| 46 | + console.log({ allAdult }) |
| 47 | + |
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 | + |
| 60 | + // comments.splice(index, 1) |
38 | 61 |
|
| 62 | + // This way we are creating a new array from the comments array and excluding index from that new array |
| 63 | + const newComments = [ |
| 64 | + ...comments.slice(0, index), |
| 65 | + ...comments.slice(index + 1) |
| 66 | + ] |
| 67 | + console.table(newComments) |
| 68 | + |
39 | 69 | </script>
|
40 | 70 | </body>
|
41 | 71 | </html>
|
0 commit comments