|
| 1 | +//Array Methods |
| 2 | + |
| 3 | +//concant() |
| 4 | +const array11 = ["a", "b", "c"]; |
| 5 | +const array21 = ["d", "e", "f"]; |
| 6 | +const array31 = array11.concat(array21); |
| 7 | +console.log(array31); // Expected output: Array ["a", "b", "c", "d", "e", "f"] |
| 8 | +const result1 = array1.concat(array2, array3); |
| 9 | + |
| 10 | +_______________________________________________________________________________________________________________________________________________________________________________________________________ |
| 11 | +//copyWithin() |
| 12 | +//Copies a part of one array to another array, while doing so overwrites the copied array instead of changing the number of elements. |
| 13 | +const array12 = ["a", "b", "c", "d", "e"]; |
| 14 | +console.log(array12.copyWithin(0, 3, 4)); // Expected output: Array ["d", "b", "c", "d", "e"] |
| 15 | +console.log(array12.copyWithin(1, 3)); // Expected output: Array ["d", "d", "e", "d", "e"] |
| 16 | + |
| 17 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 18 | +//every() |
| 19 | +const array13 = [1, 30, 39, 29, 10, 13]; |
| 20 | +console.log(array13.every((eleman) => eleman !== 0)); //true |
| 21 | +const isBelowThreshold = (currentValue) => currentValue < 10; |
| 22 | +console.log(array13.every(isBelowThreshold)); //false |
| 23 | + |
| 24 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 25 | +//filter() |
| 26 | +const words = ["spray", "limit", "elite", "exuberant", "destruction", "present"]; |
| 27 | +const result4 = words.filter((word) => word.length > 6); |
| 28 | +console.log(result4); // Expected output: Array ["exuberant", "destruction", "present"] |
| 29 | +const fruits = ["apple", "banana", "grapes", "mango", "orange"]; |
| 30 | +/** |
| 31 | + * Filter array items based on search criteria (query) |
| 32 | + */ |
| 33 | +function filterItems(arr, query) { |
| 34 | + return arr.filter((el) => el.toLowerCase().includes(query.toLowerCase())); |
| 35 | +} |
| 36 | +console.log(filterItems(fruits, "ap")); // ['apple', 'grapes'] |
| 37 | +console.log(filterItems(fruits, "an")); // ['banana', 'mango', 'orange'] |
| 38 | +const array = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; |
| 39 | +function isPrime(num) { |
| 40 | + for (let i = 2; num > i; i++) { |
| 41 | + if (num % i === 0) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + } |
| 45 | + return num > 1; |
| 46 | +} |
| 47 | +console.log(array.filter(isPrime)); // [2, 3, 5, 7, 11, 13] |
| 48 | + |
| 49 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 50 | +//forEach() |
| 51 | +const array123 = ["a", "b", "c"]; |
| 52 | +array123.forEach((element) => console.log(element)); |
| 53 | +array123.forEach((element, index) => console.log(index + " : " + element)); |
| 54 | +//As a result of the **map** loop, a new array is created and filled with the results of the operations performed in the loop, but **forEach** does not return a value. |
| 55 | +const dizi = [1, 2, 3, 4]; |
| 56 | +const yeniDizi = dizi.map(function (eleman) { |
| 57 | + return eleman * 2; |
| 58 | +}); |
| 59 | + |
| 60 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 61 | +//indexOf() |
| 62 | +const beasts = ["ant", "bison", "camel", "duck", "bison"]; // Expected output: 1 |
| 63 | +// Searching starting from the 2nd index. (that is, it does not see the bison in the 1st index) |
| 64 | +console.log(beasts.indexOf("bison", 2)); // Expected output: 4 |
| 65 | +console.log(beasts.indexOf("giraffe")); // Expected output: -1 |
| 66 | + |
| 67 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 68 | +//lastIndexOf() |
| 69 | +//Returns the last index of the element searched in the array. Useful in arrays where there is more than one identical element. |
| 70 | +const animals1 = ["Dodo", "Tiger", "Penguin", "Dodo"]; |
| 71 | +console.log(animals1.lastIndexOf("Dodo")); // Expected output: 3 |
| 72 | +console.log(animals1.lastIndexOf("Tiger")); // Expected output: 1 |
| 73 | + |
| 74 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 75 | +//map() |
| 76 | +const array14 = [1, 4, 9, 16]; |
| 77 | +// Pass a function to map |
| 78 | +const map1 = array14.map((x) => x * 2); |
| 79 | +console.log(map1); // Expected output: Array [2, 8, 18, 32] |
| 80 | + |
| 81 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 82 | +//reduce() |
| 83 | +//It is an array method used to obtain results by combining or summing all elements by looping over an array. |
| 84 | +const array15 = [1, 2, 3, 4]; |
| 85 | +const initialValue = 0; //başlangıç değeri |
| 86 | +const sum1 = array15.reduce((acc, curr) => acc + curr, initialValue); |
| 87 | +//return 0+1+2+3+4 = 10 |
| 88 | +const array155 = [1, 2, 3, 4]; |
| 89 | +const sum2 = array1.reduce((prev, current) => { |
| 90 | + return prev + current; |
| 91 | +}, 2); |
| 92 | +console.log(sum); // 0 start from 2 + 1 + 2 + 3 + 4 = 12 |
| 93 | + |
| 94 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 95 | +//reduceRight() |
| 96 | +//Same as reduce just reverses from the end of the array to the beginning. |
| 97 | +const array16 = [0, 1, 2, 3, 4]; |
| 98 | +let sum3 = array16.reduceRight((prev, current) => prev + " - " + current); |
| 99 | +console.log(sum3); |
| 100 | +("4-3-2-1-0"); |
| 101 | + |
| 102 | +const array166 = [ |
| 103 | + [0, 1], |
| 104 | + [2, 3], |
| 105 | + [4, 5], |
| 106 | +]; |
| 107 | +const result6 = array166.reduceRight((accumulator, currentValue) => accumulator.concat(currentValue)); |
| 108 | +console.log(result6); // Expected output: Array [4, 5, 2, 3, 0, 1] |
| 109 | + |
| 110 | +const array1666 = [ |
| 111 | + [0, 1], |
| 112 | + [2, 3], |
| 113 | + [4, 5], |
| 114 | +]; |
| 115 | +const result66 = array1666 |
| 116 | + .map((arr) => arr.reduce((prev, current) => prev + current)) |
| 117 | + .reduce((prev, current) => prev + current); |
| 118 | +console.log(result66); // Expected output: 15 |
| 119 | + |
| 120 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 121 | +//reverse() |
| 122 | +const array17 = ["one", "two", "three"]; |
| 123 | +const reversed1 = array17.reverse(); |
| 124 | +console.log(reversed1); //result ["three", "two", "one"]``` |
| 125 | + |
| 126 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 127 | +//toReverse() |
| 128 | +//Reverses the applied array without changing it. |
| 129 | +const array18 = ["one", "two", "three"]; |
| 130 | +const reversed2 = array18.reverse(); |
| 131 | +console.log(reversed2); //result ["three", "two", "one"] |
| 132 | +console.log(array1); // ['one', 'two', 'three'] |
| 133 | + |
| 134 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 135 | +//slice() |
| 136 | +const animals = ["ant", "bison", "camel", "duck", "elephant"]; |
| 137 | +console.log(animals.slice(2)); // Expected output: Array ["camel", "duck", "elephant"] |
| 138 | +console.log(animals.slice(2, 4)); // Expected output: Array ["camel", "duck"] |
| 139 | +console.log(animals.slice(1, 5)); // Expected output: Array ["bison", "camel", "duck", "elephant"] |
| 140 | +console.log(animals.slice(-2)); // Expected output: Array ["duck", "elephant"] |
| 141 | +console.log(animals.slice(2, -1)); // Expected output: Array ["camel", "duck"] |
| 142 | + |
| 143 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 144 | +//some() |
| 145 | +//It is used to return true if the specified condition in the array is met at least once, and false if it is never met. |
| 146 | +[0, 1, 2, 3, 4] |
| 147 | + .some((element) => element > 2) //return true |
| 148 | + [(0, 1, 2, 3, 4)].some((element) => element % 2 === 0) //return true |
| 149 | + [(0, 1, 2, 3, 4)].some((element) => element > 5); //return false |
| 150 | + |
| 151 | +function cbFunc(element) { |
| 152 | + return element > 2; |
| 153 | +} |
| 154 | +[0, 1, 2, 3, 4].some(cbFunc); //return true |
| 155 | + |
| 156 | +function cbThree(element, index, arr) { |
| 157 | + console.log(index + " : " + element); |
| 158 | + return element.length > 7; |
| 159 | +} |
| 160 | + |
| 161 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 162 | +//sort() |
| 163 | +const months = ["Ocak", "Mart", "Şubat", "Nisan", "Ali"]; |
| 164 | +months.sort(); |
| 165 | +console.log(months); // return ["Ali","Mart","Nisan","Ocak","Şubat"] |
| 166 | + |
| 167 | +const array19 = [1, 30, 4, 21, 100000]; |
| 168 | +array1.sort(); |
| 169 | +console.log(array19); // Expected output: Array [1, 100000, 21, 30, 4] |
| 170 | + |
| 171 | +//correct orderlet numbers = [40, 100, 1, 5, 25, 10]; |
| 172 | +numbers.sort(function (a, b) { |
| 173 | + return a - b; //Sorts by saying a - b, if the value of a is greater than b, it can be subtracted. |
| 174 | +}); |
| 175 | + |
| 176 | +console.log(numbers); // [1, 5, 10, 25, 40, 100] |
| 177 | + |
| 178 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 179 | +//toSort() |
| 180 | +//Sorts back a new array without changing the original array. |
| 181 | +let numbers11 = [40, 100, 1, 5, 25, 10]; |
| 182 | +let newArray = numbers11.toSort(function (a, b) { |
| 183 | + return a - b; //Sorts by saying a - b, if the value of a is greater than b, it can be subtracted. |
| 184 | +}); |
| 185 | +console.log(newArray); //[1, 5, 10, 25, 40, 100] |
| 186 | + |
| 187 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 188 | +//splice() |
| 189 | +//array.splice(start, deleteCount, item1, item2, ...); |
| 190 | +let fruits2 = ["elma", "armut", "portakal"]; |
| 191 | +fruits2.splice(2, 0, "muz", "çilek"); |
| 192 | +console.log(fruits); //["elma", "armut", "muz", "çilek", "portakal"] |
| 193 | +let numbers = [10, 20, 30, 40, 50]; |
| 194 | +numbers.splice(2, 2); |
| 195 | +console.log(numbers); //[10, 20, 50] |
| 196 | +let colors = ["kırmızı", "mavi", "yeşil", "sarı"]; |
| 197 | +colors.splice(1, 1, "turuncu"); |
| 198 | +console.log(colors); //["kırmızı", "turuncu", "yeşil", "sarı"] |
| 199 | + |
| 200 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 201 | +//entries() |
| 202 | +//An array or object method that returns an array of key-value pairs of each element in an array or an object. Returns a new array. |
| 203 | +const fruits3 = ["apple", "banana", "orange"]; |
| 204 | +const entries = fruits3.entries(); |
| 205 | +for (const entry of entries) { |
| 206 | + console.log(entry); |
| 207 | +} |
| 208 | +// [0, 'apple'] |
| 209 | +// [1, 'banana'] |
| 210 | +// [2, 'orange'] |
| 211 | + |
| 212 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 213 | +//fill() |
| 214 | +const array10 = [1, 2, 3, 4]; |
| 215 | +console.log(array10.fill(0, 2, 4)); |
| 216 | +// Array [1, 2, 0, 0] |
| 217 | +console.log(array10.fill(5, 1)); |
| 218 | +// Array [1, 5, 5, 5] |
| 219 | +console.log(array10.fill(6)); |
| 220 | +//Array [6, 6, 6, 6] |
| 221 | + |
| 222 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 223 | +//find() |
| 224 | +const array111 = [5, 12, 8, 130, 44]; |
| 225 | +const found1 = array1.find((element) => element > 10); |
| 226 | +console.log(found1); // Expected output: 12 |
| 227 | + |
| 228 | +const array112 = [ |
| 229 | + { id: 1, name: "kadir" }, |
| 230 | + { id: 2, name: "süleyman" }, |
| 231 | + { id: 3, name: "coşkun" }, |
| 232 | +]; |
| 233 | + |
| 234 | +const found2 = array112.find(({ id }) => id > 1); |
| 235 | +console.log(found); // Expected output: return object {id: 2, name: "süleyman"} |
| 236 | + |
| 237 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 238 | +///findIndex() |
| 239 | +const array113 = [5, 12, 8, 130, 44]; |
| 240 | +const isLargeNumber1 = (element) => element > 13; |
| 241 | +console.log(array1.findIndex(isLargeNumber1)); // Expected output: 3 |
| 242 | + |
| 243 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 244 | +//findLast() |
| 245 | +const array114 = [5, 12, 50, 130, 44]; |
| 246 | +const found = array114.find((element) => element > 45); |
| 247 | +console.log(found); // Expected output: 130 |
| 248 | + |
| 249 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 250 | +//findLastIndex() |
| 251 | +const array115 = [5, 12, 50, 130, 44]; |
| 252 | +const isLargeNumber = (element) => element > 45; |
| 253 | +console.log(array115.findLastIndex(isLargeNumber)); // Expected output: 3 |
| 254 | + |
| 255 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 256 | +//includes() |
| 257 | +const pets = ["cat", "dog", "bat", 12]; |
| 258 | +console.log(pets.includes(12)); //true |
| 259 | +console.log(pets.includes("a")); //true |
| 260 | +console.log([0, 1, , 2].includes(undefined)); //true |
| 261 | + |
| 262 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 263 | +//join() |
| 264 | +const elements = ["Fire", "Air", "Water"]; |
| 265 | +console.log(elements.join()); // "Fire,Air,Water" |
| 266 | +console.log(elements.join("")); // "FireAirWater" |
| 267 | +console.log(elements.join("-")); // "Fire-Air-Water" |
| 268 | +console.log([1, undefined, 3].join()); // '1,,3' |
| 269 | + |
| 270 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 271 | +//keys() |
| 272 | +const array1 = ["a", "b", , "c"]; |
| 273 | +const ite = Object.keys(array1); |
| 274 | +console.log(ite); // ["0","1","3"] |
| 275 | +const ite2 = Object.keys(array1); |
| 276 | +console.log(...ite2); // "0","1","2" |
| 277 | + |
| 278 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 279 | +//toLocaleString() |
| 280 | +let date = new Date(); |
| 281 | +console.log(date); //return "2023-07-31T06:39:18.979Z" |
| 282 | +console.log(date.toLocaleString("tr-TR")); //return "31.07.2023 09:39:18" |
| 283 | + |
| 284 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 285 | +//pop() |
| 286 | +const myArray1 = [1, 2, 3, 4, 5]; |
| 287 | +const removedElement = myArray1.pop(); |
| 288 | +console.log(removedElement); // 5 (kaldırılan eleman) |
| 289 | +console.log(myArray1); // [1, 2, 3, 4] (kalan dizinin son hali) |
| 290 | +const emptyArray = []; |
| 291 | +console.log(emptyArray.pop()); // undefined |
| 292 | + |
| 293 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 294 | +//shift() |
| 295 | +const myArray2 = [1, 2, 3, 4, 5]; |
| 296 | +console.log(myArray2.shift()); // 1 (kaldırılan eleman) |
| 297 | +console.log(myArray2); // [2, 3, 4, 5] (kalan dizinin son hali) |
| 298 | +console.log(emptyArray.shift()); // undefined |
| 299 | + |
| 300 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 301 | +//push() |
| 302 | +const myArray3 = [1, 2, 3, 4]; |
| 303 | +console.log(myArray3.push(5, 6)); // 6 (yeni dizi uzunluğu) |
| 304 | +console.log(myArray3); // [1, 2, 3, 4, 5, 6] (kalan dizinin son hali) |
| 305 | +const myArray4 = [1, 2]; |
| 306 | +const additionalItems4 = [3, 4, 5]; |
| 307 | +myArray4.push(...additionalItems); |
| 308 | +console.log(myArray4); // [1, 2, 3, 4, 5]``` |
| 309 | + |
| 310 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 311 | +//unshift() |
| 312 | +const myArray = [3, 4, 5]; |
| 313 | +console.log(myArray.unshift(1, 2)); // [1, 2, 3, 4, 5] |
| 314 | +const additionalItems = [0, -1]; |
| 315 | +myArray.unshift(...additionalItems); |
| 316 | + |
0 commit comments