1+ // ## Array Cardio Day 2
2+
3+ const people = [ {
4+ name : 'Wes' ,
5+ year : 1988
6+ } ,
7+ {
8+ name : 'Kait' ,
9+ year : 1986
10+ } ,
11+ {
12+ name : 'Irv' ,
13+ year : 1970
14+ } ,
15+ {
16+ name : 'Lux' ,
17+ year : 2015
18+ }
19+ ] ;
20+
21+ const comments = [ {
22+ text : 'Love this!' ,
23+ id : 523423
24+ } ,
25+ {
26+ text : 'Super good' ,
27+ id : 823423
28+ } ,
29+ {
30+ text : 'You are the best' ,
31+ id : 2039842
32+ } ,
33+ {
34+ text : 'Ramen is my fav food ever' ,
35+ id : 123523
36+ } ,
37+ {
38+ text : 'Nice Nice Nice!' ,
39+ id : 542328
40+ }
41+ ] ;
42+
43+ // Some and Every Checks
44+ // Array.prototype.some() // is at least one person 19 or older?
45+ console . log ( "Is at least one person 19 or older?" ) ;
46+ const isAdult = people . some ( person => ( new Date ( ) ) . getFullYear ( ) - person . year >= 19 ) ;
47+ console . log ( {
48+ isAdult
49+ } ) ;
50+ // Array.prototype.every() // is everyone 19 or older?
51+ console . log ( "Is everyone 19 or older?" ) ;
52+
53+ const isAllAdult = people . every ( person => ( new Date ( ) ) . getFullYear ( ) - person . year >= 19 ) ;
54+ console . log ( {
55+ isAllAdult
56+ } ) ;
57+ // Array.prototype.find()
58+ // Find is like filter, but instead returns just the one you are looking for
59+ // find the comment with the ID of 823423
60+ console . log ( "Find the comment with the ID of 823423" ) ;
61+
62+ const comment = comments . find ( comment => comment . id === 823423 ) ;
63+ console . log ( comment ) ;
64+ // Array.prototype.findIndex()
65+ // Find the comment with this ID
66+ console . table ( comments ) ;
67+ console . log ( "Find the comment Index with the ID of 823423" ) ;
68+
69+ const index = comments . findIndex ( comment => comment . id === 823423 ) ;
70+ console . log ( {
71+ index
72+ } ) ;
73+
74+
75+ // delete the comment with the ID of 823423
76+ console . log ( "Delete the comment with the ID of 823423" ) ;
77+ //comments.splice(index, 1);
78+ // Alternatice that keeps the original array.
79+ const newComments = [
80+ ...comments . slice ( 0 , index ) ,
81+ ...comments . slice ( index + 1 )
82+ ] ;
83+ console . table ( comments ) ;
84+ console . table ( newComments ) ;
0 commit comments