-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethodsOfArrayInJs.js
266 lines (183 loc) · 6.49 KB
/
methodsOfArrayInJs.js
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Array Methods in JavaScript
const arrOne = [1, 1, 3, 3, 5];
const arrOfOb = [
{ name: "Arafat", age: 27, id: 5 },
{ name: "Rifat", age: 20, id: 8 },
{ name: "Samir", age: 27, id: 10 },
];
// ForEach Method
console.group("%cForEach method", "color: blue; font-size: 20px;");
// Log values where the value equals the index
arrOne.forEach((num, index) => {
if (num === index) {
console.log(`Value of index ${index} = ${num}`);
}
});
// Log details of specific objects in a table
arrOfOb.forEach(({ name, age, id }) => {
if (name.toLowerCase() === "arafat" && age === 27) {
console.table([{ name, age, id }]);
}
});
console.groupEnd();
// Map Method
console.group("%cMap method", "color: blue; font-size: 20px;");
console.log(arrOne.map((num, i) => num * i));
console.log(["1", "2", "3", "4"].map(Number));
console.table(
arrOfOb.map(({ name, age }) => ({
name,
age,
}))
);
console.groupEnd();
// Filter Method
console.group("%cFilter method", "color: blue; font-size: 20px;");
console.log(arrOne.filter((num) => num > 2));
console.log(arrOfOb.filter(({ id }) => id !== 5));
console.groupEnd();
// Reduce Method
console.group("%cReduce method", "color: blue; font-size: 20px;");
console.log(
"Sum of array: ",
arrOne.reduce((acc, num) => acc + num, 0)
);
console.log(
"Max value from array: ",
arrOne.reduce((acc, num) => (acc < num ? num : acc), arrOne[0])
);
console.log(
"Min value from array: ",
arrOne.reduce((acc, num) => (num < acc ? num : acc), arrOne[0])
);
console.log(
"Product of age and id: ",
arrOfOb.reduce((acc, { age, id }) => acc + age * id, 0)
);
console.groupEnd();
// Slice Method
console.group("%cSlice method", "color: blue; font-size: 20px;");
console.log(arrOne.slice(2));
console.log(arrOne.slice(1, 3));
console.log(arrOne.slice(-2));
console.groupEnd();
// Splice Method
console.group("%cSplice method", "color: blue; font-size: 20px;");
const arrTwo = [1, 2, 3, 4, 5];
const arrThree = [1, 2, 3, 4, 5];
arrTwo.splice(1, 2, 4, 5);
arrThree.splice(1, 2);
console.log(arrTwo);
console.log(arrThree);
console.groupEnd();
// Sort Method
console.group("%cSort method", "color: blue; font-size: 20px;");
const strOfArr = ["gold", "apple", "cow", "love"];
console.log(strOfArr.sort());
console.log(arrOne.sort((a, b) => a - b));
console.log(arrOne.sort((a, b) => b - a));
console.log("1314".split("").sort().join(""));
console.groupEnd();
// Reverse Method
console.group("%cReverse method", "color: blue; font-size: 20px;");
console.log(arrOne.reverse());
console.log(strOfArr.reverse());
console.log("cba".split("").reverse().join(""));
console.groupEnd();
// Concat Method
console.group("%cConcat method", "color: blue; font-size: 20px;");
console.log(arrOne.concat(7, 8, [2, 3, 4], arrThree));
console.log(["Hi", "Hello"].concat("Hey", "Bye"));
console.groupEnd();
// Fill Method
console.group("%cFill method", "color: blue; font-size: 20px;");
console.log([1, 2, 3, 4].fill(0));
console.log([1, 2, 3, 4].fill(0, 1, 3));
console.log([1, 2, 3, 4].fill(0, 2));
console.log([1, 2, 3, 4].fill(2, 1, 3));
console.groupEnd();
// Includes Method
console.group("%cIncludes method", "color: blue; font-size: 20px;");
console.log([1, 3, 4, 5].includes(3));
console.log(["Hi", "Hello", "Hey"].includes("Hello"));
console.log(["Hi", "Hello", "Hey"].includes("Ei"));
console.groupEnd();
// IndexOf Method
console.group("%cIndexOf method", "color: blue; font-size: 20px;");
console.log([6, 5, 4, 3, 2, 2].indexOf(2));
console.log(["Hi", "Hello", "Hey", "Hello"].indexOf("Hello"));
console.log(["Hi", "Hello", "Hey"].indexOf("Ei"));
console.groupEnd();
// LastIndexOf Method
console.group("%cLastIndexOf method", "color: blue; font-size: 20px;");
console.log([6, 5, 4, 3, 2, 2].lastIndexOf(2));
console.log(["Hi", "Hello", "Hey", "Hello"].lastIndexOf("Hello"));
console.log(["Hi", "Hello", "Hey"].lastIndexOf("Ei"));
console.groupEnd();
// Every Method
console.group("%cEvery method", "color: blue; font-size: 20px;");
console.log([1, 2, 3, 4].every((num) => num > 0));
console.log([1, 2, 3, 4].every((num) => num > 2));
console.groupEnd();
// Some Method
console.group("%cSome method", "color: blue; font-size: 20px;");
console.log([1, 2, 3, 4].some((num) => num > 0));
console.log([1, 2, 3, 4].some((num) => num > 2));
console.log([1, 2, 3, 4].some((num) => num > 4));
console.groupEnd();
// Find Method
console.group("%cFind method", "color: blue; font-size: 20px;");
console.log([1, 2, 3, 4].find((num) => num === 2));
console.log(arrOfOb.find(({ name }) => name.toLowerCase() === "arafat"));
console.groupEnd();
// FindIndex Method
console.group("%cFindIndex method", "color: blue; font-size: 20px;");
console.log([1, 2, 3, 4].findIndex((num) => num === 2));
console.log(arrOfOb.findIndex(({ name }) => name.toLowerCase() === "arafat"));
console.groupEnd();
// Array.from Method
console.group("%cArray.from method", "color: blue; font-size: 20px;");
console.log(Array.from({ length: 5 }, (_, index) => index + 1));
console.log(Array.from("Hello"));
console.log(Array.from("1234", Number));
console.log(Array.from("1234", (num) => Number(num)));
console.log(Array.from(new Set([1, 2, 3, 3, 3])));
console.groupEnd();
// Array.of Method
console.group("%cArray.of method", "color: blue; font-size: 20px;");
console.log(Array.of(1, 2, 3, 4));
console.log(Array.of("Hi", "Hello", "Hey"));
console.groupEnd();
// Array.isArray Method
console.group("%cArray.isArray method", "color: blue; font-size: 20px;");
console.log(Array.isArray([1, 2]));
console.log(Array.isArray(["Hi"]));
console.log(Array.isArray(1, 3));
console.log(Array.isArray("Hi"));
console.groupEnd();
// Join Method
console.group("%cJoin method", "color: blue; font-size: 20px;");
console.log(["Hi", " Samir"].join(","));
console.groupEnd();
// Flat Method
console.group("%cFlat method", "color: blue; font-size: 20px;");
const arrFive = [[[[[[[[[2]]], 4]]], 5], 6], 7];
console.log(arrFive.flat());
console.log(arrFive.flat(1));
console.log(arrFive.flat(2));
console.log(arrFive.flat(Infinity));
console.groupEnd();
// At Method
console.group("%cAt method", "color: blue; font-size: 20px;");
console.log([1, 2, 3].at(2));
console.log(["Hi", "Hello", "Hey"].at(0));
console.groupEnd();
// CopyWithin Method
console.group("%cCopyWithin method", "color: blue; font-size: 20px;");
console.log([1, 2, 3, 4, 5].copyWithin(1, 2, 4));
console.log([1, 2, 3, 4, 5].copyWithin(1, 2));
console.groupEnd();
// ToString Method
console.group("%cToString method", "color: blue; font-size: 20px;");
console.log([1, 2, 3].toString());
console.groupEnd();