@@ -5,16 +5,16 @@ console.log(array.flat(2)); // [ 1, 2, 3, [ 4 ], [ 5 ] ]
55
66// #2 Turning this array into a new array: [ 'Hello young grasshopper!', 'you are', 'learning fast!' ]
77const greeting = [
8- [ ' Hello' , ' young' , ' grasshopper!' ] ,
9- [ ' you' , ' are' ] ,
10- [ ' learning' , ' fast!' ]
8+ [ " Hello" , " young" , " grasshopper!" ] ,
9+ [ " you" , " are" ] ,
10+ [ " learning" , " fast!" ] ,
1111] ;
1212
13- console . log ( greeting . flatMap ( x => x . join ( ' ' ) ) ) ; // [ 'Hello young grasshopper!', 'you are', 'learning fast!' ]
13+ console . log ( greeting . flatMap ( ( x ) => x . join ( " " ) ) ) ; // [ 'Hello young grasshopper!', 'you are', 'learning fast!' ]
1414
1515//#3 Turning the greeting array above into a string: 'Hello young grasshopper you are learning fast!'
1616
17- console . log ( greeting . flatMap ( x => x . join ( ' ' ) ) . join ( ' ' ) ) ; // Hello young grasshopper! you are learning fast!
17+ console . log ( greeting . flatMap ( ( x ) => x . join ( " " ) ) . join ( " " ) ) ; // Hello young grasshopper! you are learning fast!
1818
1919//#4 Turning the trapped 3 number into: [3]
2020const trapped = [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ 3 ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ;
@@ -23,7 +23,7 @@ console.log(trapped.flat(Infinity)); // [ 3 ]
2323// Infintiy is actually a LARGE number in JavaScipt. It represents the maximum amount of memory that we can hold for a number! For more here: https://riptutorial.com/javascript/example/2337/infinity-and--infinity
2424
2525//#5 Cleaning up this email to have no whitespaces. Make the answer be in a single line (return a new string):
26- const userEmail3 = ' cannotfillemailformcorrectly@gmail.com ' ;
26+ const userEmail3 = " cannotfillemailformcorrectly@gmail.com " ;
2727
2828console . log ( userEmail3 . trimEnd ( ) . trimStart ( ) ) ; // cannotfillemailformcorrectly@gmail .com
2929
@@ -34,9 +34,72 @@ const usersArray = Object.entries(users);
3434
3535//#7 changing the output array of the above to have the user's IDs multiplied by 2 -- Should output:[ [ 'user1', 36546 ], [ 'user2', 185666 ], [ 'user3', 180630 ] ]
3636
37- updatedUsersArray = usersArray . map ( user => [ user [ 0 ] , user [ 1 ] * 2 ] ) ;
37+ updatedUsersArray = usersArray . map ( ( user ) => [ user [ 0 ] , user [ 1 ] * 2 ] ) ;
3838
3939//#8 changing the output array of #7 back into an object with all the users IDs updated to their new version. Should output: { user1: 36546, user2: 185666, user3: 180630 }
4040
4141const updatedUsers = Object . fromEntries ( updatedUsersArray ) ;
4242console . log ( updatedUsers ) ; // { user1: 36546, user2: 185666, user3: 180630 }
43+
44+ // Array.flat() && Array.flatMap
45+ const jobs = [
46+ [ "👮🏻" , "💂🏻" ] ,
47+ [ "👷🏻♂️" , "🤴🏻" ] ,
48+ ] ;
49+ const flatJobs = jobs . flat ( ) ; // same as: const flatJobs = jobs.flat(1);
50+
51+ console . log ( flatJobs ) ; // ['👮🏻', '💂🏻', '👷🏻♂️', '🤴🏻']
52+
53+ const jobsList = [
54+ [ "👮🏻" , "💂🏻" ] ,
55+ [ "👷🏻♂️" , "🤴🏻" , [ "🎅🏻" , [ "🦸🏻" ] , "🦹🏻♀️" ] ] ,
56+ ] ;
57+ const flatJobsList = jobsList . flat ( Infinity ) ; // Infinity: flatten an array of arbitrary depth
58+
59+ console . log ( flatJobsList ) ; // ['👮🏻', '💂🏻', '👷🏻♂️', '🤴🏻', '🎅🏻', '🦸🏻', '🦹🏻♀️']
60+
61+ const names = [ "police" , "guard" , "builder" , "princess" ] ;
62+
63+ const mappedOnly = flatJobs . map ( ( job , index ) => [ job , names [ index ] ] ) ;
64+ const mappedAndFlatten = flatJobs . flatMap ( ( job , index ) => [ job , names [ index ] ] ) ;
65+
66+ console . log ( mappedOnly ) ; // [['👮🏻', 'police'], ['💂🏻', 'guard'], ['👷🏻♂️', 'builder'], ['🤴🏻', 'princess']]
67+ console . log ( mappedAndFlatten ) ; // ['👮🏻', 'police', '💂🏻', 'guard', '👷🏻♂️', 'builder', '🤴🏻', 'princess']
68+
69+ // From Entries
70+ const fruits = { lemon : "🍋" , pineapple : "🍍" , mango : "🥭" , apple : "🍏" } ;
71+ const entries = Object . entries ( fruits ) ;
72+
73+ console . log ( Object . entries ( fruits ) ) ; // [['lemon', '🍋'],['pineapple', '🍍'],['mango', '🥭'],['apple', '🍏']]
74+ console . log ( Object . fromEntries ( entries ) ) ; // {lemon: '🍋', pineapple: '🍍', mango: '🥭', apple: '🍏'}
75+
76+ // String.trimStart() & String.trimEnd()
77+ const fruitsList = " 🥭🍏🍍 " ;
78+ console . log ( fruitsList . trimEnd ( ) ) ; // " 🥭🍏🍍";
79+ console . log ( fruitsList . trimStart ( ) ) ; // "🥭🍏🍍 ";
80+
81+ // Stable Array.prototype.sort()
82+ const fruitsArr = [
83+ { name : "🥝" , units : 12 } ,
84+ { name : "🫐" , units : 14 } ,
85+ { name : "🍏" , units : 16 } ,
86+ { name : "🥬" , units : 15 } ,
87+ { name : "🍊" , units : 18 } ,
88+ { name : "🍐" , units : 19 } ,
89+ ] ;
90+
91+ // Create our own sort criteria function:
92+ const sortCriteria = ( fruit1 , fruit2 ) => fruit1 . units - fruit2 . units ;
93+
94+ // Perform stable ES10 sort:
95+ const sorted = fruitsArr . sort ( sortCriteria ) ;
96+
97+ console . log ( sorted ) ;
98+ // [
99+ // { name: "🥝", units: 12 },
100+ // { name: "🫐", units: 14 },
101+ // { name: "🥬", units: 15 },
102+ // { name: "🍏", units: 16 },
103+ // { name: "🍊", units: 18 },
104+ // { name: "🍐", units: 19 },
105+ // ];
0 commit comments