GeezerVison is showing github.com — Added array sorting and new ES6 samples · reprogram2098/JavaScript-Lessons@cb8447b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard - Display the source diff
- Display the rich diff
File renamed without changes. Collapse file Copy file name to clipboard File renamed without changes. Collapse file Copy file name to clipboard +3Lines changed: 3 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Title | | 2 | + | | 3 | +Description |
Collapse file Copy file name to clipboard +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +"use strict"; | | 2 | + | | 3 | +let numbers = [2, 4, 7, 9]; | | 4 | + | | 5 | +// from() creates a new array from an existing one | | 6 | +let numbers2 = Array.from(numbers); | | 7 | +console.log(numbers2); | | 8 | + | | 9 | +// fill() fills an array with specified value | | 10 | +let bacon = [1, 2, 3, 4, 5]; | | 11 | +bacon.fill(0); | | 12 | +console.log(bacon); | | 13 | + | | 14 | +// we can pass in the starting index as a second parameter | | 15 | +let tuna = [1, 2, 3, 4, 5]; | | 16 | +tuna.fill(68, 2); | | 17 | +console.log(tuna); | | 18 | + | | 19 | + |
Collapse file Copy file name to clipboard +3Lines changed: 3 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Title | | 2 | + | | 3 | +Description |
Collapse file Copy file name to clipboard +63Lines changed: 63 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +"use strict"; | | 2 | + | | 3 | +let items = [1, 10, 17, 18, 2, 7, 3, 19, 14, 5]; | | 4 | +console.log(items.reverse()); | | 5 | + | | 6 | +// not what we wanted, because it sorts the numbers as if they are all strings | | 7 | +console.log(items.sort()); | | 8 | + | | 9 | +console.log('--------------------'); | | 10 | + | | 11 | +/* | | 12 | + * When the sort() method compares two values, it sends the values to the compare function, and sorts the values | | 13 | + * according to the returned (negative, zero, positive) value. | | 14 | + * | | 15 | + * EXAMPLE: | | 16 | + * When comparing 40 and 100, the sort() method calls the compare function(40,100) | | 17 | + * The function calculates 40-100, and returns -60 (a negative value) | | 18 | + * The sort function will sort 40 as a value lower than 100 | | 19 | + * */ | | 20 | +items.sort(function (a, b) { | | 21 | + return a - b; | | 22 | +}); | | 23 | +console.log(items); | | 24 | + | | 25 | +console.log('--------------------'); | | 26 | + | | 27 | +// reverse sort | | 28 | +items.sort(function (a, b) { | | 29 | + return b - a; | | 30 | +}); | | 31 | +console.log(items); | | 32 | + | | 33 | +console.log('--------------------'); | | 34 | + | | 35 | +// can also do this | | 36 | +function compare(a, b) { | | 37 | + if (a < b) | | 38 | + return -1; | | 39 | + if (a > b) | | 40 | + return 1; | | 41 | + return 0; | | 42 | +} | | 43 | +let items2 = [1, 10, 17, 18, 2, 7, 3, 19, 14, 5]; | | 44 | +console.log(items2.sort(compare)); | | 45 | + | | 46 | +console.log('--------------------'); | | 47 | + | | 48 | +// custom sorting | | 49 | +let friends = [ | | 50 | + {name: 'Bucky', age: 50}, | | 51 | + {name: 'Sally', age: 40}, | | 52 | + {name: 'Tommy', age: 70}, | | 53 | + {name: 'Wendy', age: 20}, | | 54 | + {name: 'Chris', age: 60} | | 55 | +]; | | 56 | +function compareAges(a, b){ | | 57 | + if (a.age < b.age) | | 58 | + return -1; | | 59 | + if (a.age > b.age) | | 60 | + return 1; | | 61 | + return 0; | | 62 | +} | | 63 | +console.log(friends.sort(compareAges)); |
|