|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>Document</title> |
| 6 | +</head> |
| 7 | +<body> |
| 8 | + <script> |
| 9 | + // ## Array Cardio Day 2 |
| 10 | + |
| 11 | + const people = [ |
| 12 | + { name: 'Wes', year: 1988 }, |
| 13 | + { name: 'Kait', year: 1986 }, |
| 14 | + { name: 'Irv', year: 1970 }, |
| 15 | + { name: 'Lux', year: 2015 } |
| 16 | + ]; |
| 17 | + |
| 18 | + const comments = [ |
| 19 | + { text: 'Love this!', id: 523423 }, |
| 20 | + { text: 'Super good', id: 823423 }, |
| 21 | + { text: 'You are the best', id: 2039842 }, |
| 22 | + { text: 'Ramen in my fav food ever', id: 123523 }, |
| 23 | + { text: 'Nice Nice Nice!', id: 542328 } |
| 24 | + ]; |
| 25 | + |
| 26 | + // Some and Every Checks |
| 27 | + // Array.prototype.some() // is at least one person 19? |
| 28 | + |
| 29 | + // the straightforward way to do it |
| 30 | + // const isAdult = people.some(function(person){ |
| 31 | + // const currentYear = (new Date()).getFullYear(); |
| 32 | + // if(currentYear - person.year >= 19){ |
| 33 | + // return true; |
| 34 | + // } |
| 35 | + // }); |
| 36 | + |
| 37 | + // the fancier way to do it |
| 38 | + const isAdult = people.some(person => ((new Date()).getFullYear()) - person.year >= 19); |
| 39 | + |
| 40 | + console.log({isAdult}); |
| 41 | + |
| 42 | + // Array.prototype.every() // is everyone 19? |
| 43 | + const allAdult = people.every(person => ((new Date()).getFullYear()) - person.year >= 19); |
| 44 | + |
| 45 | + console.log({allAdult}); |
| 46 | + |
| 47 | + // Array.prototype.find() |
| 48 | + // Find is like filter, but instead returns just the one you are looking for |
| 49 | + // find the comment with the ID of 823423 |
| 50 | + const comment = comments.find(comment => comment.id === 823423); |
| 51 | + |
| 52 | + console.log(comment); |
| 53 | + |
| 54 | + // Array.prototype.findIndex() |
| 55 | + // Find the comment with this ID |
| 56 | + |
| 57 | + const findComment = comments.findIndex(comment => comment.id ===823423); |
| 58 | + console.log(findComment); |
| 59 | + |
| 60 | + // delete the comment with the ID of 823423 |
| 61 | + |
| 62 | + comments.splice(1, 1); |
| 63 | + |
| 64 | + console.table(comments); |
| 65 | + |
| 66 | + |
| 67 | + </script> |
| 68 | +</body> |
| 69 | +</html> |
0 commit comments