|
| 1 | +## ⚑ Arrays: |
| 2 | + |
| 3 | +Arrays are generally [Non-Primitive Data Types](./data-types.md#-non-primitive-data-types). |
| 4 | + |
| 5 | +Array in JavaScript are collections of value list, where the values are any data type. They represent list of values. |
| 6 | + |
| 7 | +### ☴ Overview: |
| 8 | +1. [Array Methods](#-array-methods) |
| 9 | +2. [Array Iteration](#-array-iteration) |
| 10 | + |
| 11 | +### ✦ Array Methods: |
| 12 | +JavaScript provide a variety of methods for manipulating and working with arrays. |
| 13 | + |
| 14 | +**Common Array Methods:** |
| 15 | +- `push():` Adds elements to the end of an array. |
| 16 | +- `pop():` Removes the last element from an array and returns it. |
| 17 | +- `shift():` Removes the first element from an array and returns it. |
| 18 | +- `unshift():` Adds elements to the beginning of an array. |
| 19 | +- `slice():` Creates a new array containing a portion of the original array. |
| 20 | +- `splice():` Removes or replaces elements in an array. |
| 21 | +- `concat():` Combines multiple arrays into a new array. |
| 22 | +- `join():` Joins elements of an array into a string. |
| 23 | +- `reverse():` Reverses the order of elements in an array. |
| 24 | +- `sort():` Sorts elements in an array. |
| 25 | +- `forEach():` Executes a function for each element in an array. |
| 26 | +- `map():` Creates a new array by applying a function to each element of the original array. |
| 27 | +- `filter():` Creates a new array containing elements that meet a certain condition. |
| 28 | +- `reduce():` Applies a function to each element in an array to reduce it to a single value. |
| 29 | +- `indexOf():` Returns the index of the first occurrence of an element in an array. |
| 30 | +- `lastIndexOf():` Returns the index of the last occurrence of an element in an array. |
| 31 | +- `includes():` Checks if an array contains a specified element. |
| 32 | +- `find():` Returns the first element in an array that meets a certain condition. |
| 33 | +- `findIndex():` Returns the index of the first element in an array that meets a certain condition. |
| 34 | + |
| 35 | +```javascript |
| 36 | +let fruits = ["apple", "banana", "orange"]; |
| 37 | + |
| 38 | +// Adding elements |
| 39 | +fruits.push("grape"); |
| 40 | +fruits.unshift("mango"); |
| 41 | +console.log(fruits); // Output: (5) ['mango', 'apple', 'banana', 'orange', 'grape'] |
| 42 | + |
| 43 | +// Removing elements |
| 44 | +let removedFruit = fruits.pop(); |
| 45 | +console.log(removedFruit); // Output: grape |
| 46 | +console.log(fruits); // Output: (4) ['mango', 'apple', 'banana', 'orange'] |
| 47 | + |
| 48 | +// Creating a new array |
| 49 | +let slicedFruits = fruits.slice(1, 3); |
| 50 | +console.log(slicedFruits); // Output: (2) ['apple', 'banana'] |
| 51 | +console.log(fruits); // Output: (4) ['mango', 'apple', 'banana', 'orange'] |
| 52 | + |
| 53 | +// Joining elements |
| 54 | +let fruitString = fruits.join(", "); |
| 55 | +console.log(fruitString); // Output: mango, apple, banana, orange |
| 56 | +console.log(fruits); // Output: (4) ['mango', 'apple', 'banana', 'orange'] |
| 57 | + |
| 58 | +// Sorting elements |
| 59 | +fruits.sort(); |
| 60 | +console.log(fruits); // Output: (4) ['apple', 'banana', 'mango', 'orange'] |
| 61 | + |
| 62 | +// Iterating over elements |
| 63 | +fruits.forEach(function(fruit) { |
| 64 | + console.log(fruit); |
| 65 | +}); |
| 66 | +/* |
| 67 | +apple |
| 68 | +banana |
| 69 | +mango |
| 70 | +orange |
| 71 | +*/ |
| 72 | + |
| 73 | +// Mapping elements |
| 74 | +let fruitLengths = fruits.map(function(fruit) { |
| 75 | + return fruit.length; |
| 76 | +}); |
| 77 | + |
| 78 | +console.log(fruitLengths); // Output: (4) [5, 6, 5, 6] |
| 79 | +``` |
| 80 | + |
| 81 | +### ✦ Array Iteration: |
| 82 | +Array iteration involves traversing each element of an array to perform operations on them. There are several ways to iterate over arrays in JavaScript. |
| 83 | + |
| 84 | +**Iteration of Array:** |
| 85 | +- for loop: |
| 86 | +```javascript |
| 87 | +for (let i = 0; i < fruits.length; i++) { |
| 88 | + console.log(fruits[i]); |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +- for...in loop: |
| 93 | +```javascript |
| 94 | +for (let index in fruits) { |
| 95 | + console.log(fruits[index]); |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +- for...of loop: |
| 100 | +```javascript |
| 101 | +for (let fruit of fruits) { |
| 102 | + console.log(fruit); |
| 103 | +} |
| 104 | +``` |
| 105 | +- Array methods as loop: |
| 106 | + - forEach() |
| 107 | + - map() |
| 108 | + - filter() |
| 109 | + - reduce() |
| 110 | + |
| 111 | + *These methods provide different ways to iterate over arrays and perform operations on their elements, depending on the specific needs.* |
| 112 | + |
| 113 | +**Note:** When comes to loop, iterations, setTimeout and setInterval. Use [`let`](./variables.md#-let). That treats as a new [`variable`](./variables.md) on each iteration. But [`var`](./variables.md#-var) overwrites that memory place. |
| 114 | + |
| 115 | +```javascript |
| 116 | +/* let usage */ |
| 117 | +let fruits = ["apple", "banana", "orange"]; |
| 118 | +for(let fruit of fruits) { |
| 119 | + setTimeout(() => { console.log(fruit) }, 1000); |
| 120 | +} |
| 121 | +/* |
| 122 | +apple |
| 123 | +banana |
| 124 | +orange |
| 125 | +*/ |
| 126 | + |
| 127 | +/* var usage */ |
| 128 | +let fruits = ["apple", "banana", "orange"]; |
| 129 | +for(var fruit of fruits) { |
| 130 | + setTimeout(() => { console.log(fruit) }, 1000); |
| 131 | +} |
| 132 | +/* |
| 133 | +orange |
| 134 | +orange |
| 135 | +orange |
| 136 | +*/ |
| 137 | +``` |
| 138 | + |
| 139 | +--- |
| 140 | +[⇪ To Top](#-arrays) |
| 141 | + |
| 142 | +[❮ Previous Topic](./objects.md)   [Next Topic ❯](./asynchronous-javascript.md) |
| 143 | + |
| 144 | +[⌂ Goto Home Page](../README.md) |
0 commit comments