|
| 1 | +/*###########################################################################*/ |
| 2 | +//Different ways to declaring an array |
| 3 | +const arr = [1, 2, 3]; |
| 4 | +console.log(arr); |
| 5 | + |
| 6 | +const newArray = new Array(5); //fixed array size of 5 |
| 7 | +console.log(newArray); |
| 8 | + |
| 9 | +const withourNewArray = Array(5); //fixed array size of 5 |
| 10 | +console.log(withourNewArray); |
| 11 | + |
| 12 | +const ofArray = Array.of(1, 2); |
| 13 | +console.log(ofArray); |
| 14 | + |
| 15 | +const li = document.querySelectorAll("li"); //it will give array like object |
| 16 | +console.log(li); |
| 17 | + |
| 18 | +const fromArray = Array.from(li); //convert an array to array like object |
| 19 | +console.log(fromArray); |
| 20 | +//Different ways to declaring an array end |
| 21 | +/*###########################################################################*/ |
| 22 | + |
| 23 | +/*###########################################################################*/ |
| 24 | +//Data can store in an array |
| 25 | +const personalData = [ |
| 26 | + 30, |
| 27 | + "Meet", |
| 28 | + { moreDetail: ["M.P.", "Bhopal", "462002"] }, |
| 29 | + true, |
| 30 | +]; |
| 31 | +console.log(personalData[2].moreDetail[1]); |
| 32 | + |
| 33 | +for (const i of personalData) { |
| 34 | + console.log(i); |
| 35 | +} |
| 36 | + |
| 37 | +const matrix = [ |
| 38 | + [1, 5], |
| 39 | + [14, 3], |
| 40 | + [7, 9], |
| 41 | +]; |
| 42 | + |
| 43 | +for (const i of matrix) { |
| 44 | + for (const j of i) { |
| 45 | + console.log(j); |
| 46 | + } |
| 47 | +} |
| 48 | +//Different ways to declaring an array end |
| 49 | +/*###########################################################################*/ |
| 50 | + |
| 51 | +/*###########################################################################*/ |
| 52 | +//push, pop, shift and unshift |
| 53 | +const books = ["C in Depth", "C++"]; |
| 54 | +console.log(books); |
| 55 | +books.push("Cracking the Coding Interview"); //add element at last and returns lenght |
| 56 | +console.log(books); |
| 57 | +books.unshift("Data Structures & Algorithms"); //add element at start and returns lenght |
| 58 | +console.log(books); |
| 59 | +books.unshift("Think Like a Monk"); |
| 60 | +books.push("Marvels"); |
| 61 | +console.log(books); |
| 62 | + |
| 63 | +//removes first element |
| 64 | +const firstRemovedElement = books.shift(); |
| 65 | +console.log(`Removed first element -> ${firstRemovedElement}`); |
| 66 | +console.log(books); |
| 67 | + |
| 68 | +//removes last element |
| 69 | +const lastRemovedElement = books.pop(); |
| 70 | +console.log(`Removed last element -> ${lastRemovedElement}`); |
| 71 | +console.log(books); |
| 72 | +//push, pop, shift and unshift end |
| 73 | + |
| 74 | +//splice method and it will return removed element(s) |
| 75 | +//first arg is index of array, 2nd arg is delete and 3rd arg add element on index |
| 76 | +books.splice(3, 0, "JavaScript", "Python", "Summer Vaccation"); |
| 77 | +console.log(books); |
| 78 | + |
| 79 | +//first arg is index and second arg is for delete that index |
| 80 | +books.splice(5, 1); |
| 81 | +console.log(books); |
| 82 | + |
| 83 | +books.splice( |
| 84 | + 0, |
| 85 | + 0, |
| 86 | + "Think Like a Monk", |
| 87 | + "How to Influence People", |
| 88 | + "Life Saver" |
| 89 | +); |
| 90 | +console.log(books); |
| 91 | + |
| 92 | +books.splice(0, 3); //It will delete from 0 to 2 |
| 93 | +console.log(books); |
| 94 | + |
| 95 | +books.splice(3); //It will delete all elements from index 3 |
| 96 | +console.log(books); |
| 97 | + |
| 98 | +books.splice(-2, 1); //It will start from end of the array and remove it |
| 99 | +console.log(books); |
| 100 | + |
| 101 | +books.splice(0); //It will delete all elements from an array |
| 102 | +console.log(books); |
| 103 | +//splice end |
| 104 | +/*###########################################################################*/ |
| 105 | + |
| 106 | +/*###########################################################################*/ |
| 107 | +//slice |
| 108 | +const testResult = [1, 4, 5, 3.3, -6, 47, "Calculus", -8, 13.44, "Algebra"]; |
| 109 | +console.log(testResult); |
| 110 | + |
| 111 | +const storedResult = testResult.slice(); //copy an array not reference |
| 112 | +testResult.push(44); |
| 113 | +console.log(storedResult, testResult); |
| 114 | + |
| 115 | +//returns a brand new array, copy from begining |
| 116 | +const result = testResult.slice(0, 3); |
| 117 | +console.log(result); |
| 118 | + |
| 119 | +//returns a brand new array, copy from end |
| 120 | +const lastResult = testResult.slice(-4); |
| 121 | +console.log(lastResult); |
| 122 | +//slice end |
| 123 | +/*###########################################################################*/ |
| 124 | + |
| 125 | +/*###########################################################################*/ |
| 126 | +//concate |
| 127 | +//it will also return brand new copy and brand new address |
| 128 | +const arr1 = [1, 2, 3, 4, 5, 6]; |
| 129 | +const arr2 = [7, 8, 9, 10, 11, 12]; |
| 130 | +const concateArr = arr1.concat(arr2); |
| 131 | +console.log(concateArr); |
| 132 | + |
| 133 | +const concateAnotherArr = arr1.concat([ |
| 134 | + [11, 14, 15, 19, 20], |
| 135 | + [31, 41, 22, 65], |
| 136 | +]); |
| 137 | + |
| 138 | +console.log(concateAnotherArr); |
| 139 | + |
| 140 | +const oneMoreConcateArr = arr2.concat([13, 14, 15, 16, 17]); |
| 141 | +console.log(oneMoreConcateArr); |
| 142 | +//concate end |
| 143 | +/*###########################################################################*/ |
| 144 | + |
| 145 | +/*###########################################################################*/ |
| 146 | +//indexOf and lastIndexOf |
| 147 | +const mobileModels = [ |
| 148 | + "Redmi Note 7", |
| 149 | + "One Plus+", |
| 150 | + "Samsung M3", |
| 151 | + "One Plus+", |
| 152 | + "Nokia 5530", |
| 153 | +]; |
| 154 | +console.log(mobileModels); |
| 155 | +//indexOf returns first occurence index of found element, even that element exist more than one times |
| 156 | +//indexOf searching starts from left |
| 157 | +let found = mobileModels.indexOf("One Plus+", 0); |
| 158 | + |
| 159 | +if (found >= 0) { |
| 160 | + console.log(`Found Item on index ${found}`); |
| 161 | +} |
| 162 | + |
| 163 | +//indexOf returns last occurence index of found element, even that element exist more than one times |
| 164 | +//lastIndexOf searching starts from right |
| 165 | +found = mobileModels.lastIndexOf("One Plus+", -1); |
| 166 | + |
| 167 | +if (found >= 0) { |
| 168 | + console.log(`Found Item on index ${found}`); |
| 169 | +} |
| 170 | + |
| 171 | +const person = [{ language: "English" }, { language: "Hindi" }]; |
| 172 | +//the below line won't work coz object has address and we check different address in indexOf |
| 173 | +found = person.indexOf({ language: "English" }); //it will give -1 |
| 174 | + |
| 175 | +if (found < 0) { |
| 176 | + console.log(`Not found Item`); |
| 177 | +} |
| 178 | +//indexOf and lastIndexOf end |
| 179 | +/*###########################################################################*/ |
| 180 | + |
| 181 | +/*###########################################################################*/ |
| 182 | +//find and findIndex |
| 183 | +const districtName = [ |
| 184 | + { city: "Bhopal" }, |
| 185 | + { city: "Indore" }, |
| 186 | + { city: "Banglore" }, |
| 187 | +]; |
| 188 | + |
| 189 | +//find doesn't create a copy |
| 190 | +//find return matching item |
| 191 | +let city = districtName.find((district, idx, cities) => { |
| 192 | + return district.city === "Bhopal"; |
| 193 | +}); |
| 194 | + |
| 195 | +console.log(city); |
| 196 | + |
| 197 | +//findIndex return index of the item |
| 198 | +city = districtName.findIndex((district, idx, cities) => { |
| 199 | + return district.city === "Banglore"; |
| 200 | +}); |
| 201 | + |
| 202 | +console.log(city); |
| 203 | +//find and findIndex end |
| 204 | +/*###########################################################################*/ |
| 205 | + |
| 206 | +/*###########################################################################*/ |
| 207 | +//includes |
| 208 | +const percentage = [44, 85, 99, 34, 65, 71]; |
| 209 | + |
| 210 | +//includes return true if element has in an array other wise false |
| 211 | +if (percentage.includes(99, 0)) { |
| 212 | + console.log("Found"); |
| 213 | +} |
| 214 | + |
| 215 | +if (!percentage.includes(15)) { |
| 216 | + console.log("Not Found"); |
| 217 | +} |
| 218 | +//includes end |
| 219 | +/*###########################################################################*/ |
0 commit comments