|
| 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 | + //const isAdult = people.some(function(person) { |
| 29 | + // const currentYear = (new Date()).getFullYear(); |
| 30 | + // if (currentYear - person.year >= 19) { |
| 31 | + // return true |
| 32 | + // } |
| 33 | + // }) ;/ |
| 34 | + |
| 35 | + // console.log({isAdult}); |
| 36 | + |
| 37 | + const isAdult = people.some (person => { |
| 38 | + const currentYear = (new Date()).getFullYear(); |
| 39 | + return currentYear - person.year >= 19; |
| 40 | + }); |
| 41 | + |
| 42 | + console.log({isAdult}); |
| 43 | + |
| 44 | + // Array.prototype.every() // is everyone 19? |
| 45 | + const allAdults = people.every (person => { |
| 46 | + const currentYear = (new Date()).getFullYear(); |
| 47 | + return currentYear - person.year >= 19; |
| 48 | + }); |
| 49 | + |
| 50 | + console.log({allAdults}); |
| 51 | + // Array.prototype.find() |
| 52 | + // Find is like filter, but instead returns just the first item it finds |
| 53 | + // find the comment with the ID of 823423 |
| 54 | + const comment = comments.find(comment => { |
| 55 | + return comment.id === 823423 |
| 56 | + }); |
| 57 | + |
| 58 | + console.log(comment); |
| 59 | + |
| 60 | + // Array.prototype.findIndex() |
| 61 | + // Find the comment with this ID |
| 62 | + // delete the comment with the ID of 823423 |
| 63 | + |
| 64 | + const index = comments.findIndex(comment => { |
| 65 | + return comment.id === 823423 |
| 66 | + }); |
| 67 | + |
| 68 | + console.log(index); |
| 69 | + |
| 70 | + // const newComments = comments.splice(index, 1); //splice modifies the original array |
| 71 | + |
| 72 | + const newComments = [ |
| 73 | + ...comments.slice(0, index), |
| 74 | + ...comments.slice(index + 1) |
| 75 | + ]; |
| 76 | + |
| 77 | + console.log(newComments); |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + </script> |
| 83 | +</body> |
| 84 | +</html> |
0 commit comments