|
| 1 | +const log = console.log; |
| 2 | + |
| 3 | +export function concat() { |
| 4 | + // Create arrays of monovalves and bivalves |
| 5 | + let monovalves = ['abalone', 'conch']; |
| 6 | + let bivalves = ['oyster', 'mussel', 'clam']; |
| 7 | + |
| 8 | + // Concatenate them together into shellfish variable |
| 9 | + let shellfish = monovalves.concat(bivalves); |
| 10 | + log(shellfish); |
| 11 | +} |
| 12 | + |
| 13 | +export function join() { |
| 14 | + let fish = ['piranha', 'barracuda', 'koi', 'eel']; |
| 15 | + // Join the elements of an array into a string |
| 16 | + |
| 17 | + // If separator omitted, the array elements are separated with a comma. |
| 18 | + let fishString = fish.join(); |
| 19 | + log(fishString); |
| 20 | + |
| 21 | + let fishStringWithHyphen = fish.join('-'); |
| 22 | + log(fishStringWithHyphen); |
| 23 | +} |
| 24 | + |
| 25 | +export function slice() { |
| 26 | + let fish = ['piranha', 'barracuda', 'koi', 'eel']; |
| 27 | + // Slice a new array from 2 to 4 |
| 28 | + let fishWithShortNames = fish.slice(2, 4); |
| 29 | + log(fishWithShortNames); |
| 30 | + |
| 31 | + // Slice a new array from 2 to the end of the array |
| 32 | + fishWithShortNames = fish.slice(2); |
| 33 | + |
| 34 | + log(fishWithShortNames); |
| 35 | +} |
| 36 | + |
| 37 | +export function indexOf() { |
| 38 | + let fish = ['piranha', 'barracuda', 'koi', 'barracuda']; |
| 39 | + // Find the first instance of an element |
| 40 | + let index = fish.indexOf('barracuda'); |
| 41 | + log(index); |
| 42 | + |
| 43 | + //If the given argument is a value that does not exist in the array, it will return -1. |
| 44 | + index = fish.indexOf('akash'); |
| 45 | + log(index); |
| 46 | +} |
| 47 | + |
| 48 | +export function lastIndexOf() { |
| 49 | + let fish = ['piranha', 'barracuda', 'koi', 'barracuda']; |
| 50 | + // Find the first instance of an element |
| 51 | + let index = fish.lastIndexOf('barracuda'); |
| 52 | + log(index); |
| 53 | + |
| 54 | + //If the given argument is a value that does not exist in the array, it will return -1. |
| 55 | + index = fish.lastIndexOf('akash'); |
| 56 | + log(index); |
| 57 | +} |
| 58 | + |
| 59 | +export function pop() { |
| 60 | + const fruits = ['Banana', 'Orange', 'Apple', 'Mango']; |
| 61 | + log('fruits before pop => ', fruits); |
| 62 | + let poppedFruit = fruits.pop(); |
| 63 | + log('fruits after pop', fruits); |
| 64 | + |
| 65 | + // The pop() method returns the value that was "popped out": |
| 66 | + log('popped Fruit => ', poppedFruit); |
| 67 | +} |
| 68 | + |
| 69 | +export function push() { |
| 70 | + const fruits = ['Banana', 'Orange', 'Apple', 'Mango']; |
| 71 | + log('fruits before PUSH => ', fruits); |
| 72 | + let newLength = fruits.push('KIWI'); |
| 73 | + log('fruits after PUSH => ', fruits); |
| 74 | + |
| 75 | + // The push() method returns the new array length: |
| 76 | + log('popped Fruit => ', newLength); |
| 77 | +} |
| 78 | + |
| 79 | +export function shift() { |
| 80 | + // The shift() method removes the first array element and "shifts" all other elements to a lower index. |
| 81 | + |
| 82 | + const fruits = ['Banana', 'Orange', 'Apple', 'Mango']; |
| 83 | + log('fruits before shift => ', fruits); |
| 84 | + fruits.shift(); |
| 85 | + log('fruits after shift => ', fruits); |
| 86 | +} |
| 87 | + |
| 88 | +export function unshift() { |
| 89 | + // The unshift() method adds a new element to an array (at the beginning), and "unshifts" older elements: |
| 90 | + |
| 91 | + const fruits = ['Banana', 'Orange', 'Apple', 'Mango']; |
| 92 | + log('fruits before unshift => ', fruits); |
| 93 | + let newLength = fruits.unshift('KIWI'); |
| 94 | + log('fruits after unshift => ', fruits); |
| 95 | + |
| 96 | + // The unshift() method returns the new array length. |
| 97 | + log('NEW LENGTH AFTET UNSHIFT => ', newLength); |
| 98 | +} |
| 99 | + |
| 100 | +export function deleteElement() { |
| 101 | + // The unshift() method adds a new element to an array (at the beginning), and "unshifts" older elements: |
| 102 | + |
| 103 | + const fruits = ['Banana', 'Orange', 'Apple', 'Mango']; |
| 104 | + log('fruits before delete => ', fruits); |
| 105 | + delete fruits[0]; |
| 106 | + log('fruits after delete => ', fruits); |
| 107 | +} |
| 108 | + |
| 109 | +export function addWithSplice() { |
| 110 | + // splice(index number, number of items to remove, items to add) |
| 111 | + |
| 112 | + let fish = ['piranha', 'barracuda', 'koi', 'eel']; |
| 113 | + |
| 114 | + // Splice a new item number into index position 1 |
| 115 | + fish.splice(1, 0, 'manta ray'); |
| 116 | + |
| 117 | + log(fish); |
| 118 | +} |
| 119 | + |
| 120 | +export function removeWithSplice() { |
| 121 | + // splice(index number, number of items to remove, items to add) |
| 122 | + let fish = ['piranha', 'barracuda', 'koi', 'eel']; |
| 123 | + |
| 124 | + // Remove two items, starting at index position 1 |
| 125 | + fish.splice(1, 2); |
| 126 | + |
| 127 | + log(fish); |
| 128 | +} |
| 129 | + |
| 130 | +export function addremoveWithSplice() { |
| 131 | + // splice(index number, number of items to remove, items to add) |
| 132 | + let fish = ['piranha', 'barracuda', 'koi', 'eel']; |
| 133 | + |
| 134 | + // Remove two items and add one |
| 135 | + fish.splice(1, 2, 'manta ray'); |
| 136 | + |
| 137 | + log(fish); |
| 138 | + |
| 139 | + fish.splice(1, 0, 'first-fish', 'second-fish'); |
| 140 | + log(fish); |
| 141 | +} |
| 142 | + |
| 143 | +export function fill() { |
| 144 | + let fish = ['piranha', 'barracuda', 'koi', 'eel']; |
| 145 | + |
| 146 | + // Replace all values in the array with "shark" |
| 147 | + fish.fill('shark'); |
| 148 | + |
| 149 | + log(fish); |
| 150 | +} |
| 151 | + |
| 152 | +export function sort() { |
| 153 | + let fish = ['piranha', 'barracuda', 'koi', 'eel']; |
| 154 | + // Sort items in array |
| 155 | + fish.sort(); |
| 156 | + log(fish); |
| 157 | + |
| 158 | + //Since sort() is based on the first unicode character, it will sort uppercase items before lowercase. |
| 159 | + fish = ['piranha', 'barracuda', 'Koi', 'eel']; |
| 160 | + fish.sort(); |
| 161 | + log(fish); |
| 162 | + |
| 163 | + //Numbers come before both uppercase and lowercase characters. |
| 164 | + fish = ['piranha', 'barracuda', 'Koi', '1 eel']; |
| 165 | + fish.sort(); |
| 166 | + log(fish); |
| 167 | + |
| 168 | + // sort() will not sort an array of numbers by size by default. Instead, it will only check the first character in the number. |
| 169 | + let numbers = [42, 23, 16, 15, 4, 8]; |
| 170 | + numbers.sort(); |
| 171 | + log(numbers); |
| 172 | + |
| 173 | + // In order to sort numbers properly, you could create a comparison function as an argument. |
| 174 | + // Function to sort numbers by size |
| 175 | + const sortNumerically = (a, b) => { |
| 176 | + return a - b; |
| 177 | + }; |
| 178 | + numbers.sort(sortNumerically); |
| 179 | + log(numbers); |
| 180 | + log(numbers.sort((a, b) => b - a)); |
| 181 | + |
| 182 | + //object array sort |
| 183 | + let employees = [ |
| 184 | + { name: 'John', salary: 90000, hireDate: new Date(2011, 10, 30) }, |
| 185 | + { name: 'David', salary: 75000, hireDate: new Date(2011, 11, 30) }, |
| 186 | + { name: 'Ana', salary: 80000, hireDate: new Date(2022, 10, 30) }, |
| 187 | + ]; |
| 188 | + employees.sort(function (x, y) { |
| 189 | + return x.salary - y.salary; |
| 190 | + }); |
| 191 | + |
| 192 | + console.table(employees); |
| 193 | + |
| 194 | + // by name |
| 195 | + employees.sort(function (x, y) { |
| 196 | + let a = x.name.toUpperCase(), |
| 197 | + b = y.name.toUpperCase(); |
| 198 | + return a == b ? 0 : a > b ? 1 : -1; |
| 199 | + }); |
| 200 | + |
| 201 | + console.table(employees); |
| 202 | + |
| 203 | + // by date |
| 204 | + employees.sort(function (x, y) { |
| 205 | + let a = new Date(x.hireDate), |
| 206 | + b = new Date(y.hireDate); |
| 207 | + return a - b; |
| 208 | + }); |
| 209 | + |
| 210 | + console.table(employees); |
| 211 | +} |
0 commit comments