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