|
| 1 | +// ## Array Cardio Day 2 |
| 2 | + |
| 3 | +const people = [ |
| 4 | + { name: "Wes", year: 1988 }, |
| 5 | + { name: "Kait", year: 1986 }, |
| 6 | + { name: "Irv", year: 1970 }, |
| 7 | + { name: "Lux", year: 2015 }, |
| 8 | +]; |
| 9 | + |
| 10 | +const comments = [ |
| 11 | + { text: "Love this!", id: 523423 }, |
| 12 | + { text: "Super good", id: 823423 }, |
| 13 | + { text: "You are the best", id: 2039842 }, |
| 14 | + { text: "Ramen is my fav food ever", id: 123523 }, |
| 15 | + { text: "Nice Nice Nice!", id: 542328 }, |
| 16 | +]; |
| 17 | + |
| 18 | +// Some and Every Checks |
| 19 | +// Array.prototype.some() // is at least one person 19 or older? |
| 20 | +const isAdult = people.some((person) => { |
| 21 | + const currentYear = new Date().getFullYear(); |
| 22 | + return currentYear - person.year >= 19; |
| 23 | +}); |
| 24 | + |
| 25 | +console.log(isAdult); |
| 26 | + |
| 27 | +// Array.prototype.every() // is everyone 19 or older? |
| 28 | +const allAdults = people.every((person) => { |
| 29 | + const currentYear = new Date().getFullYear(); |
| 30 | + return currentYear - person.year >= 19; |
| 31 | +}); |
| 32 | + |
| 33 | +console.log(allAdults); |
| 34 | + |
| 35 | +// Array.prototype.find() |
| 36 | +// Find is like filter, but instead returns just the one you are looking for |
| 37 | +// find the comment with the ID of 823423 |
| 38 | +const findComment = comments.find((comment) => { |
| 39 | + return comment.id === 823423 ? comment.text : false; |
| 40 | +}); |
| 41 | + |
| 42 | +console.log(findComment); |
| 43 | + |
| 44 | +// Array.prototype.findIndex() |
| 45 | +// Find the comment with this ID |
| 46 | +// delete the comment with the ID of 823423 |
| 47 | +const index = comments.findIndex((comment) => comment.id === 823423); |
| 48 | +console.log(index); |
| 49 | +// comments.splice(index,1); |
| 50 | + |
| 51 | +const newComments = [...comments.slice(0, index), ...comments.slice(index + 1)]; |
| 52 | +console.table(newComments); |
| 53 | +console.table(comments); |
0 commit comments