|
26 | 26 |
|
27 | 27 | // Some and Every Checks |
28 | 28 | // Array.prototype.some() // is at least one person 19? |
29 | | - // Array.prototype.every() // is everyone 19? |
| 29 | + //will return true if there is at least one adult in people array who is over 19 |
| 30 | + |
| 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 | + // console.log(isAdult); |
| 38 | + //or |
| 39 | + |
| 40 | + |
| 41 | + const isAdult = people.some(person => { |
| 42 | + const currentYear = (new Date()).getFullYear(); |
| 43 | + return currentYear - person.year >= 19; |
| 44 | + }); |
| 45 | + console.log(isAdult); |
| 46 | + |
| 47 | + // Array.prototype.every() // is everyone 19? |
| 48 | + const allAdult = people.every(person => { |
| 49 | + const currentYear = (new Date()).getFullYear(); |
| 50 | + return currentYear - person.year >= 19; |
| 51 | + }); |
| 52 | + console.log(allAdult); |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + // // Array.prototype.find() |
| 57 | + // const theComment = comments.find(function(comment) { |
| 58 | + // if(comment.id === 823423){ |
| 59 | + // return true; |
| 60 | + // } |
| 61 | + // }); |
| 62 | + // console.log(theComment); |
| 63 | + //or |
| 64 | + |
| 65 | + const theComment = comments.find(comment => |
| 66 | + comment.id === 823423); |
| 67 | + console.log(theComment); |
| 68 | + |
30 | 69 |
|
31 | | - // Array.prototype.find() |
32 | 70 | // Find is like filter, but instead returns just the one you are looking for |
33 | 71 | // find the comment with the ID of 823423 |
34 | 72 |
|
35 | 73 | // Array.prototype.findIndex() |
36 | 74 | // Find the comment with this ID |
37 | 75 | // delete the comment with the ID of 823423 |
38 | 76 |
|
| 77 | + const index = comments.findIndex(comment => |
| 78 | + comment.id === 823423); |
| 79 | + console.log('index', index); |
| 80 | + |
| 81 | + // comments.splice(index, 1); |
| 82 | + |
| 83 | + //popular in redux world: |
| 84 | + const newComments = [ |
| 85 | + ...comments.slice(0, index), |
| 86 | + ...comments.slice(index + 1) |
| 87 | + ]; |
| 88 | + |
| 89 | + //will build new array of comments |
| 90 | + //... will spread items into new array |
| 91 | + |
39 | 92 | </script> |
40 | 93 | </body> |
41 | 94 | </html> |
0 commit comments