-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_Array.js
More file actions
219 lines (174 loc) · 11 KB
/
Copy path10_Array.js
File metadata and controls
219 lines (174 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
let fruits = ["Apple", "Banana", "Mango", "Orange", "Grapes"];
let new_fruits = new Array("Apple", "Banana", "Mango", "Orange", "Grapes");
console.log(typeof fruits); // "object"
console.log(typeof new_fruits); // "object"
console.log(Array.isArray(fruits)); // "true"
console.log(Array.isArray(new_fruits)); // "true"
// ------------------------------------------------------------------------------------------------
// ARRAY PROPERTY :-
// .length - returns the length of an array
console.log(fruits.length); // "5"
fruits.length = 2; // setting the length of an array to a smaller value will remove elements from the end of an array (modifies original array)
console.log(fruits); // "["Apple", "Banana"]"
fruits.length = 7; // setting the length of an array to a greater value will add empty slots to the end of an array (modifies original array)
console.log(fruits); // "["Apple", "Banana", "Mango", "Orange", "Grapes", <2 empty items>]"
// ------------------------------------------------------------------------------------------------
// ARRAY METHODS :-
// Modifies The Original Array :
// .push() - add new element at the end of an array
fruits.push("Pineapple");
console.log(fruits); // "["Apple", "Banana", "Mango", "Orange", "Grapes", "Pineapple"]"
// .pop() - remove the last element of an array
fruits.pop();
console.log(fruits); // "["Apple", "Banana", "Mango", "Orange", "Grapes"]"
// .unshift() - add new elements to the beginning of an array and returns the new length of an array
let new_length = fruits.unshift("Strawberry", "Pineapple");
console.log(fruits); // "["Strawberry", "Pineapple", "Apple", "Banana", "Mango", "Orange", "Grapes"]"
console.log(new_length); // "7"
// .shift() - remove and return the first element of an array
let removed_fruit = fruits.shift();
console.log(fruits); // "["Banana", "Mango", "Orange", "Grapes"]"
console.log(removed_fruit); // "Apple"
// .splice() - add / remove / replace elements from an array
// ------------------------------------------------------------------------
// | array.splice(start_index, delete_count, item_1, item_2, item_3, ...) |
// ------------------------------------------------------------------------
fruits.splice(2, 0, "Strawberry", "Pineapple", "Lichi"); // add "Strawberry", "Pineapple" and "Lichi" at index 2 and remove 0 elements from index 2
console.log(fruits); // "["Apple", "Banana", "Strawberry", "Pineapple", "Lichi", "Mango", "Orange", "Grapes"]"
fruits.splice(2, 2, "Strawberry", "Pineapple", "Lichi"); // add "Strawberry", "Pineapple" and "Lichi" at index 2 and remove 2 elements from index 2
console.log(fruits); // "["Apple", "Banana", "Strawberry", "Pineapple", "Lichi", "Grapes"]"
fruits.splice(2, 3); // remove 3 elements from index 2
console.log(fruits); // "["Apple", "Banana"]"
fruits.splice(2, 3, "Mango", "Orange", "Grapes"); // add "Mango", "Orange", "Grapes" at index 2 and remove 3 elements from index 2
console.log(fruits); // "["Apple", "Banana", "Mango", "Orange", "Grapes"]"
// --------------------------------------------------------------------------------------
/*|*/ let removed_fruits = fruits.splice(2, 3); // returns an array of removed elements |
/*|*/ console.log(removed_fruits); // "["Mango", "Orange", "Grapes"]" |
// --------------------------------------------------------------------------------------
// .reverse() - reverse the order of an array
fruits.reverse();
console.log(fruits); // "["Grapes", "Orange", "Mango", "Banana", "Apple"]"
// .sort() - sort an array in ascending / descending order
// -----------------------------------------------------------------------
// | strings.sort((a, b) => a.localeCompare(b)); // Ascending (Strings) |
// | strings.sort((a, b) => b.localeCompare(a)); // Descending (Strings) |
// | numbers.sort((a, b) => a - b); // Ascending (Numbers) |
// | numbers.sort((a, b) => b - a); // Descending (Numbers) |
// -----------------------------------------------------------------------
fruits.sort((a, b) => a.localeCompare(b));
console.log(fruits); // "["Apple", "Banana", "Grapes", "Mango", "Orange"]"
fruits.sort((a, b) => b.localeCompare(a));
console.log(fruits); // "["Orange", "Mango", "Grapes", "Banana", "Apple"]"
// ----------------------------------------------
// Doesn't Modify The Original Array :
// .concat() - merge two or more arrays
let fresh_fruits = ["Strawberry", "Pineapple", "Lichi"];
console.log(fruits.concat(fresh_fruits)); // "["Apple", "Banana", "Mango", "Orange", "Grapes", "Strawberry", "Pineapple", "Lichi"]"
console.log(fresh_fruits.concat(fruits)); // "["Strawberry", "Pineapple", "Lichi", "Apple", "Banana", "Mango", "Orange", "Grapes"]"
// -------------------------------------------------------------------------------------------------------------------------------------------
/* |*/ console.log(fruits + fresh_fruits); // "Apple,Banana,Mango,Orange,GrapesStrawberry,Pineapple,Lichi" |
/* |*/ console.log([fruits, fresh_fruits]); // "[["Apple", "Banana", "Mango", "Orange", "Grapes"], ["Strawberry", "Pineapple", "Lichi"]]" |
/* |*/ console.log([...fruits, fresh_fruits]); // "["Apple", "Banana", "Mango", "Orange", "Grapes", ["Strawberry", "Pineapple", "Lichi"]]" |
/* |*/ console.log([...fruits, ...fresh_fruits]); // "["Apple", "Banana", "Mango", "Orange", "Grapes", "Strawberry", "Pineapple", "Lichi"]"" |
// -------------------------------------------------------------------------------------------------------------------------------------------
// .join() - convert array to string
console.log(fruits.join()); // "Apple,Banana,Mango,Orange,Grapes"
console.log(fruits.join(",")); // "Apple,Banana,Mango,Orange,Grapes"
console.log(fruits.join(", ")); // "Apple, Banana, Mango, Orange, Grapes"
console.log(fruits.join("")); // "AppleBananaMangoOrangeGrapes"
console.log(fruits.join(" ")); // "Apple Banana Mango Orange Grapes"
// .toString() - convert array to a comma-separated string (you cannot change the separator, it will always return a string with comma separated values)
console.log(fruits.toString()); // "Apple,Banana,Mango,Orange,Grapes"
console.log(fruits.toString(",")); // "Apple,Banana,Mango,Orange,Grapes"
console.log(fruits.toString(", ")); // "Apple,Banana,Mango,Orange,Grapes"
console.log(fruits.toString("")); // "Apple,Banana,Mango,Orange,Grapes"
console.log(fruits.toString(" ")); // "Apple,Banana,Mango,Orange,Grapes"
// .slice() - returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included)
fruits.slice(1, 4); // returns a new array from index 1 to index 3 (4 not included)
console.log(fruits.slice(1, 4)); // "["Banana", "Mango", "Orange"]"
console.log(fruits.slice(2)); // "["Mango", "Orange", "Grapes"]"
console.log(fruits.slice(-3)); // "["Mango", "Orange", "Grapes"]"
console.log(fruits.slice(-4, -1)); // "["Banana", "Mango", "Orange"]"
// ----------------------------------------------------------------------------------------------------------------------------------------------------
// | negative values in the slice() method means counting from the end of an array. -1 is the last element, -2 is the second last element, and so on. |
// ----------------------------------------------------------------------------------------------------------------------------------------------------
// .indexOf() - returns the index of a specified value in an array (only the first match)
console.log(fruits.indexOf("Mango")); // "2"
console.log(fruits.indexOf("Banana")); // "1"
console.log(fruits.indexOf("Pineapple")); // "-1" (if not found)
// .at() - returns the value at a specific index in an array (allows negative indexes)
console.log(fruits.at(2)); // "Mango"
console.log(fruits.at(-2)); // "Orange"
// .find() - returns the value of the first element in an array that satisfies a provided testing function
let fruit_basket = ["Apple", "Banana", "Mango", "Orange", "Grapes", "Mango", "Banana", "Apple"];
let fruit = fruit_basket.find((fruit) => fruit === "Mango");
console.log(fruit); // "Mango"
// .findIndex() - returns the index of the first element in an array that satisfies a provided testing function
let index = fruits.findIndex((fruit) => fruit === "Mango");
console.log(index); // "2"
// .includes() - tests whether an array includes a certain value among its entries and returns a boolean value
console.log(fruits.includes("Mango")); // "true"
console.log(fruits.includes("Guava")); // "false"
// .some() - tests whether at least one element in an array passes the test implemented by the provided function and returns a boolean value
let has_mango = fruits.some((fruit) => fruit === "Mango");
console.log(has_mango); // "true"
let has_guava = fruits.some((fruit) => fruit === "Guava");
console.log(has_guava); // "false"
// .every() - tests whether all elements in an array pass the test implemented by the provided function and returns a boolean value
let all_fruits_are_strings = fruits.every((fruit) => typeof fruit === "string");
console.log(all_fruits_are_strings); // "true"
let all_fruits_have_a = fruits.every((fruit) => fruit.includes("a"));
console.log(all_fruits_have_a); // "false"
// .flat() - returns a new array with all sub-array elements concatenated into it recursively up to the specified depth
// -----------------------------
// | Syntax: array.flat(depth) |
// -----------------------------
let nested_array = [1, 2, [3, 4, [5, 6]]];
console.log(nested_array.flat()); // "[1, 2, 3, 4, [5, 6]]"
console.log(nested_array.flat(0)); // "[1, 2, [3, 4, [5, 6]]]"
console.log(nested_array.flat(1)); // "[1, 2, 3, 4, [5, 6]]"
console.log(nested_array.flat(2)); // "[1, 2, 3, 4, 5, 6]"
console.log(nested_array.flat(Infinity)); // "[1, 2, 3, 4, 5, 6]"
// Iterators :
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// .map() - returns a new array with the value returned from the callback
let result = numbers.map((num) => {
return num * 2;
});
console.log(result);
// "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]"
// .filter() - returns a new array with only the true values returned from the callback
let even_numbers = numbers.filter((num) => {
return num % 2 === 0;
});
console.log(even_numbers);
// "[2, 4, 6, 8, 10]"
// .forEach() - executes a function for every element and returns undefined (no array)
numbers.forEach((num) => {
console.log(num * 2);
});
// "2
// 4
// 6
// 8
// 10
// 12
// 14
// 16
// 18
// 20"
// .reduce() - combine all the elements of an array into a single result
let sum = numbers.reduce((accumulator, current_element) => {
return accumulator + current_element;
}, 0);
console.log(sum);
// "55"
// --------------------------------------------------
// | Basic Syntax :- |
// | array.reduce((accumulator, current_value) => { |
// | return new_value; |
// | }, initial_value); |
// --------------------------------------------------
// ----------------------------------------------------------------------------
// | map(), filter(), forEach() and reduce() do not modify the original array |
// ----------------------------------------------------------------------------