1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < title > Array Cardio 💪💪</ title >
6+ </ head >
7+ < body >
8+ < p > < em > 请按 F12 查看 Console 面板</ em > 💁</ p >
9+ < p > 若无结果请刷新试试</ p >
10+ < script >
11+ // ## Array Cardio Day 2
12+ // 数组基本操作指南二
13+
14+ const people = [
15+ { name : 'Wes' , year : 1988 } ,
16+ { name : 'Kait' , year : 1986 } ,
17+ { name : 'Irv' , year : 1970 } ,
18+ { name : 'Lux' , year : 2015 }
19+ ] ;
20+
21+ const comments = [
22+ { text : 'Love this!' , id : 523423 } ,
23+ { text : 'Super good' , id : 823423 } ,
24+ { text : 'You are the best' , id : 2039842 } ,
25+ { text : 'Ramen is my fav food ever' , id : 123523 } ,
26+ { text : 'Nice Nice Nice!' , id : 542328 }
27+ ] ;
28+
29+ // Some and Every Checks
30+ // Array.prototype.some() // is at least one person 19? 是否有人超过 19 岁?
31+ const isAdult = people . some ( person => {
32+ const currentYear = ( new Date ( ) ) . getFullYear ( ) ;
33+ return currentYear - person . year >= 19 ;
34+ } ) ;
35+ console . log ( { isAdult} ) ;
36+
37+ // Array.prototype.every() // is everyone 19? 是否所有人都是成年人?
38+ const allAdult = people . every ( person => new Date ( ) . getFullYear ( ) - person . year >= 19 ) ;
39+ console . log ( { allAdult} ) ;
40+
41+ // Array.prototype.find()
42+ // Find is like filter, but instead returns just the one you are looking for
43+ // find the comment with the ID of 823423
44+ // 找到 ID 号为 823423 的评论
45+ const comment = comments . find ( comment => comment . id == 823423 ) ;
46+ console . log ( comment ) ;
47+
48+ // Array.prototype.findIndex()
49+ // Find the comment with this ID
50+ // delete the comment with the ID of 823423
51+ // 删除 ID 号为 823423 的评论
52+ const index = comments . findIndex ( comment => comment . id == 823423 ) ;
53+
54+ // 删除方法一,splice()
55+ // comments.splice(index, 1);
56+ console . table ( comments ) ;
57+ // 删除方法二,slice 拼接
58+ const newComments = [
59+ ...comments . slice ( 0 , index ) ,
60+ ...comments . slice ( index + 1 )
61+ ] ;
62+ console . table ( newComments ) ;
63+ </ script >
64+ </ body >
65+ </ html >
0 commit comments