|
| 1 | + |
| 2 | +// ========================== |
| 3 | +// Array Methods Demonstration |
| 4 | +// ========================== |
| 5 | + |
| 6 | +// ========================== |
| 7 | +// 1. push() : Add to end |
| 8 | +// ========================== |
| 9 | + |
| 10 | +// Initialize an array with food items |
| 11 | +let foodArray = ["chips", "cheese", "bread", "paneer"]; |
| 12 | +console.log("Initial foodArray:", foodArray); |
| 13 | +// Output: Initial foodArray: [ 'chips', 'cheese', 'bread', 'paneer' ] |
| 14 | + |
| 15 | +// Mutates the original array by adding items |
| 16 | +foodArray.push("milk", "onion", "corn-flour"); |
| 17 | +console.log("After push:", foodArray); |
| 18 | +// Output: After push: [ 'chips', 'cheese', 'bread', 'paneer', 'milk', 'onion', 'corn-flour' ] |
| 19 | + |
| 20 | +// Returns the new length of the array |
| 21 | +let updatedLength = foodArray.push("lettuce"); |
| 22 | +console.log("New length:", updatedLength); |
| 23 | +// Output: New length: 8 |
| 24 | + |
| 25 | +// Can accept multiple arguments |
| 26 | +foodArray.push("tomato", "cucumber"); |
| 27 | +console.log("After pushing more items:", foodArray); |
| 28 | +// Output: After pushing more items: [ 'chips', 'cheese', 'bread', 'paneer', 'milk', 'onion', 'corn-flour', 'lettuce', 'tomato', 'cucumber' ] |
| 29 | + |
| 30 | +// Handles different data types |
| 31 | +foodArray.push(42, { item: "apple" }); |
| 32 | +console.log("After pushing different types:", foodArray); |
| 33 | +// Output: After pushing different types: |
| 34 | +// [ 'chips', 'cheese', 'bread', 'paneer', 'milk', 'onion', 'corn-flour', |
| 35 | +// 'lettuce', 'tomato', 'cucumber', 42, { item: 'apple' } ] |
| 36 | + |
| 37 | +// Can push `undefined` |
| 38 | +foodArray.push(undefined); |
| 39 | +console.log("After pushing undefined:", foodArray); |
| 40 | +// Output: After pushing undefined: |
| 41 | +// [ 'chips', 'cheese', 'bread', 'paneer', 'milk', 'onion', 'corn-flour', |
| 42 | +// 'lettuce', 'tomato', 'cucumber', 42, { item: 'apple' }, undefined ] |
| 43 | + |
| 44 | +// Use in loops to add items |
| 45 | +let additionalItems = ["carrot", "spinach"]; |
| 46 | +additionalItems.forEach(item => foodArray.push(item)); |
| 47 | +console.log("After pushing items in a loop:", foodArray); |
| 48 | +// Output: After pushing items in a loop: |
| 49 | +// [ 'chips', 'cheese', 'bread', 'paneer', 'milk', 'onion', 'corn-flour', |
| 50 | +// 'lettuce', 'tomato', 'cucumber', 42, { item: 'apple' }, undefined, |
| 51 | +// 'carrot', 'spinach' ] |
| 52 | + |
| 53 | + |
| 54 | +// ========================== |
| 55 | +// 2. pop() : Delete from end & return |
| 56 | +// ========================== |
| 57 | + |
| 58 | +// Initialize an array of fruits |
| 59 | +let fruitArray = ["apple", "banana"]; |
| 60 | +console.log("Initial fruitArray:", fruitArray); |
| 61 | +// Output: Initial fruitArray: [ 'apple', 'banana' ] |
| 62 | + |
| 63 | +// Use push() to add a single element |
| 64 | +fruitArray.push("orange"); |
| 65 | +console.log("After push('orange'):", fruitArray); |
| 66 | +// Output: After push('orange'): [ 'apple', 'banana', 'orange' ] |
| 67 | + |
| 68 | +// Use push() to add multiple elements |
| 69 | +fruitArray.push("grape", "kiwi"); |
| 70 | +console.log("After push('grape', 'kiwi'):", fruitArray); |
| 71 | +// Output: After push('grape', 'kiwi'): [ 'apple', 'banana', 'orange', 'grape', 'kiwi' ] |
| 72 | + |
| 73 | +// Pushing different data types |
| 74 | +let mixedArray = [1, "two", true]; |
| 75 | +mixedArray.push(null, { key: "value" }); |
| 76 | +console.log("Mixed array after push:", mixedArray); |
| 77 | +// Output: Mixed array after push: [ 1, 'two', true, null, { key: 'value' } ] |
| 78 | + |
| 79 | +// Push on an empty array |
| 80 | +let emptyArray = []; |
| 81 | +emptyArray.push(1); |
| 82 | +console.log("Empty array after push(1):", emptyArray); |
| 83 | +// Output: Empty array after push(1): [ 1 ] |
| 84 | + |
| 85 | + |
| 86 | +// ========================== |
| 87 | +// 3. toString() : Converts array to string |
| 88 | +// ========================== |
| 89 | + |
| 90 | +// Initialize an array of colors |
| 91 | +let colorArray = ["red", "green", "blue"]; |
| 92 | +let colorString = colorArray.toString(); |
| 93 | +console.log("Array as string:", colorString); |
| 94 | +// Output: Array as string: red,green,blue |
| 95 | + |
| 96 | +// Handle nested arrays |
| 97 | +let nestedArray = [1, 2, [3, 4], 5]; |
| 98 | +console.log("Nested array as string:", nestedArray.toString()); |
| 99 | +// Output: Nested array as string: 1,2,3 |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +// ========================== |
| 104 | +// 4. concat() : Merge Arrays |
| 105 | +// ========================== |
| 106 | + |
| 107 | +// Initialize two arrays |
| 108 | +let firstArray = ["a", "b", "c"]; |
| 109 | +let secondArray = ["d", "e", "f"]; |
| 110 | +console.log("Initial firstArray:", firstArray); |
| 111 | +// Output: Initial firstArray: [ 'a', 'b', 'c' ] |
| 112 | +console.log("Initial secondArray:", secondArray); |
| 113 | +// Output: Initial secondArray: [ 'd', 'e', 'f' ] |
| 114 | + |
| 115 | +// Use concat() to merge arrays |
| 116 | +let mergedArray = firstArray.concat(secondArray); |
| 117 | +console.log("Merged array:", mergedArray); |
| 118 | +// Output: Merged array: [ 'a', 'b', 'c', 'd', 'e', 'f' ] |
| 119 | + |
| 120 | +// Concat with multiple arrays |
| 121 | +let thirdArray = ["g", "h"]; |
| 122 | +let combinedArray = firstArray.concat(secondArray, thirdArray); |
| 123 | +console.log("Combined array:", combinedArray); |
| 124 | +// Output: Combined array: [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' ] |
| 125 | + |
| 126 | +// Concat with different data types |
| 127 | +let mixedConcatArray = firstArray.concat(42, { key: "value" }); |
| 128 | +console.log("Mixed array after concat:", mixedConcatArray); |
| 129 | +// Output: Mixed array after concat: [ 'a', 'b', 'c', 42, { key: 'value' } ] |
| 130 | + |
| 131 | + |
| 132 | +// ========================== |
| 133 | +// 5. unshift() : Add to Beginning |
| 134 | +// ========================== |
| 135 | + |
| 136 | +// Initialize an array of numbers |
| 137 | +let numberArray = [2, 3, 4]; |
| 138 | +console.log("Initial numberArray:", numberArray); |
| 139 | +// Output: Initial numberArray: [ 2, 3, 4 ] |
| 140 | + |
| 141 | +// Use unshift() to add elements to the beginning |
| 142 | +numberArray.unshift(1); |
| 143 | +console.log("After unshift(1):", numberArray); |
| 144 | +// Output: After unshift(1): [ 1, 2, 3, 4 ] |
| 145 | + |
| 146 | +// Unshift multiple elements |
| 147 | +numberArray.unshift(-1, 0); |
| 148 | +console.log("After unshift(-1, 0):", numberArray); |
| 149 | +// Output: After unshift(-1, 0): [ -1, 0, 1, 2, 3, 4 ] |
| 150 | + |
| 151 | +// Unshift different data types |
| 152 | +numberArray.unshift("start", null); |
| 153 | +console.log("After unshift('start', null):", numberArray); |
| 154 | +// Output: After unshift('start', null): [ 'start', null, -1, 0, 1, 2, 3, 4 ] |
| 155 | + |
| 156 | + |
| 157 | +// ========================== |
| 158 | +// 6. shift() : Remove from Beginning & Return |
| 159 | +// ========================== |
| 160 | + |
| 161 | +// Initialize an array of fruits |
| 162 | +let fruitList = ["apple", "banana", "cherry"]; |
| 163 | +console.log("Initial fruitList:", fruitList); |
| 164 | +// Output: Initial fruitList: [ 'apple', 'banana', 'cherry' ] |
| 165 | + |
| 166 | +// Use shift() to remove the first element |
| 167 | +let removedFruit = fruitList.shift(); |
| 168 | +console.log("Removed fruit:", removedFruit); |
| 169 | +// Output: Removed fruit: apple |
| 170 | +console.log("After shift:", fruitList); |
| 171 | +// Output: After shift: [ 'banana', 'cherry' ] |
| 172 | + |
| 173 | +// Shift again |
| 174 | +let secondRemovedFruit = fruitList.shift(); |
| 175 | +console.log("Removed fruit:", secondRemovedFruit); |
| 176 | +// Output: Removed fruit: banana |
| 177 | +console.log("After another shift:", fruitList); |
| 178 | +// Output: After another shift: [ 'cherry' ] |
| 179 | + |
| 180 | +// Shift from a single-element array |
| 181 | +fruitList.shift(); |
| 182 | +console.log("After shifting the last element:", fruitList); |
| 183 | +// Output: After shifting the last element: [ ] |
| 184 | + |
| 185 | +// Shift from an empty array |
| 186 | +let emptyList = []; |
| 187 | +let removedFromEmptyList = emptyList.shift(); |
| 188 | +console.log("Removed from empty array:", removedFromEmptyList); |
| 189 | +// Output: Removed from empty array: undefined |
| 190 | +console.log("After shift on empty array:", emptyList); |
| 191 | +// Output: After shift on empty array: [ ] |
| 192 | + |
| 193 | +// ========================== |
| 194 | +// 7. slice() : Extract and return a portion of an array |
| 195 | +// slice syntax : slice( startIdx, endIdx ) |
| 196 | +// ========================== |
| 197 | + |
| 198 | +// Initialize an array of vegetables |
| 199 | +let vegArray = ["carrot", "potato", "tomato", "onion", "spinach"]; |
| 200 | +console.log("Initial vegArray:", vegArray); |
| 201 | +// Output: Initial vegArray: [ 'carrot', 'potato', 'tomato', 'onion', 'spinach' ] |
| 202 | + |
| 203 | +// Use slice() to extract a portion of the array |
| 204 | +let slicedArray = vegArray.slice(1, 3); |
| 205 | +console.log("Sliced array (1, 3):", slicedArray); |
| 206 | +// Output: Sliced array (1, 3): [ 'potato', 'tomato' ] |
| 207 | + |
| 208 | +// Slice without specifying endIdx (extract till end) |
| 209 | +let slicedToEnd = vegArray.slice(2); |
| 210 | +console.log("Sliced from index 2:", slicedToEnd); |
| 211 | +// Output: Sliced from index 2: [ 'tomato', 'onion', 'spinach' ] |
| 212 | + |
| 213 | +// Slice with negative indices |
| 214 | +let slicedNegative = vegArray.slice(-3, -1); |
| 215 | +console.log("Sliced with negative indices (-3, -1):", slicedNegative); |
| 216 | +// Output: Sliced with negative indices (-3, -1): [ 'tomato', 'onion' ] |
| 217 | + |
| 218 | +// Original array remains unchanged |
| 219 | +console.log("After slice, original array:", vegArray); |
| 220 | +// Output: After slice, original array: [ 'carrot', 'potato', 'tomato', 'onion', 'spinach' ] |
| 221 | + |
| 222 | + |
| 223 | +// ========================== |
| 224 | +// 8. splice() : Modify original array (add, remove, or replace) |
| 225 | +// syntax : splice( startIdx, delCount, newElm...) |
| 226 | +// ========================== |
| 227 | + |
| 228 | +// Initialize an array of fruits |
| 229 | +let fruitBasket = ["apple", "banana", "cherry", "date"]; |
| 230 | +console.log("Initial fruitBasket:", fruitBasket); |
| 231 | +// Output: Initial fruitBasket: [ 'apple', 'banana', 'cherry', 'date' ] |
| 232 | + |
| 233 | +// Use splice() to remove elements |
| 234 | +let removedFruits = fruitBasket.splice(1, 2); // Remove 2 elements starting at index 1 |
| 235 | +console.log("Removed fruits:", removedFruits); |
| 236 | +// Output: Removed fruits: [ 'banana', 'cherry' ] |
| 237 | +console.log("After removing fruits:", fruitBasket); |
| 238 | +// Output: After removing fruits: [ 'apple', 'date' ] |
| 239 | + |
| 240 | +// Use splice() to add elements |
| 241 | +fruitBasket.splice(1, 0, "blueberry", "grape"); // Add at index 1 without removing |
| 242 | +console.log("After adding fruits:", fruitBasket); |
| 243 | +// Output: After adding fruits: [ 'apple', 'blueberry', 'grape', 'date' ] |
| 244 | + |
| 245 | +// Use splice() to replace elements |
| 246 | +fruitBasket.splice(2, 1, "kiwi"); // Replace 1 element at index 2 |
| 247 | +console.log("After replacing an element:", fruitBasket); |
| 248 | +// Output: After replacing an element: [ 'apple', 'blueberry', 'kiwi', 'date' ] |
| 249 | + |
| 250 | +// Use splice() to clear all elements |
| 251 | +fruitBasket.splice(0, fruitBasket.length); |
| 252 | +console.log("After clearing all elements:", fruitBasket); |
| 253 | +// Output: After clearing all elements: [] |
0 commit comments