-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
643 lines (515 loc) · 14.4 KB
/
index.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/**
*Important Note:
© 2024 tapaScript. Feel free to use the content of the file to solve questions, learn things. Sharing content of this file outside of this repository requires permission from the [author](https://github.com/atapas). Also the content from this repository must not be used for any commercial digital or printed product without the author's consent. Please reach out to [Tapas Adhikary](https://www.linkedin.com/in/tapasadhikary/) for any questions.
*/
"use strict";
// How to Create an Array in JavaScript
{
const salad = ['🍅', '🍄', '🥦', '🥒', '🌽', '🥕', '🥑'];
const anotherSalad = new Array('🍅', '🍄', '🥦', '🥒', '🌽', '🥕', '🥑');
}
// How to Get Elements from an Array in JS
{
const element = array[index];
const salad = ['🍅', '🍄', '🥦', '🥒', '🌽', '🥕', '🥑'];
salad[0]; // '🍅'
salad[2]; // '🥦'
salad[5]; // '🥕'
const salad = ['🍅', '🍄', '🥦', '🥒', '🌽', '🥕', '🥑'];
const len = salad.length;
salad[len - 1]; // '🥑'
salad[len - 3]; // '🌽'
const salad = ['🍅', '🍄', '🥦', '🥒', '🌽', '🥕', '🥑'];
for(let i=0; i<salad.length; i++) {
console.log(`Element at index ${i} is ${salad[i]}`);
}
}
// How to Add Elements to an Array in JS
{
const salad = ['🍅', '🍄', '🥦', '🥒', '🌽', '🥕', '🥑'];
salad.push('🥜');
const salad = ['🍅', '🍄', '🥦', '🥒', '🌽', '🥕', '🥑'];
salad.unshift('🥜');
}
// How to Remove Elements from an Array in JS
{
const salad = ['🍅', '🍄', '🥦', '🥒', '🌽', '🥕', '🥑'];
salad.pop(); // 🥑
console.log(salad); // ['🍅', '🍄', '🥦', '🥒', '🌽', '🥕']
}
// How to Copy and Clone an Array in JS
{
const salad = ['🍅', '🍄', '🥦', '🥒', '🌽', '🥕', '🥑'];
const saladCopy = salad.slice();
console.log(saladCopy); // ['🍅', '🍄', '🥦', '🥒', '🌽', '🥕', '🥑']
salad === saladCopy; // returns false
}
// How to Determine if a Value is an Array in JS
{
Array.isArray(['🍅', '🍄', '🥦', '🥒', '🌽', '🥕', '🥑']); // returns true
Array.isArray('🍅'); // returns false
Array.isArray({ 'tomato': '🍅'}); // returns false
Array.isArray([]); // returns true
}
// Array Destructuring in JavaScript
{
let [tomato, mushroom, carrot] = ['🍅', '🍄', '🥕'];
console.log(tomato, mushroom, carrot); // Output, 🍅 🍄 🥕
let vegetables = ['🍅', '🍄', '🥕'];
let tomato = vegetables[0];
let mushroom= vegetables[1];
let carrot= vegetables[2];
}
// How to Assign a Default Value to a Variable
{
let [tomato , mushroom = '🍄'] = ['🍅'];
console.log(tomato); // '🍅'
console.log(mushroom ); // '🍄'
}
// How to Skip a Value in an Array
{
let [tomato, , carrot] = ['🍅', '🍄', '🥕'];
console.log(tomato); // '🍅'
console.log(carrot); // '🥕'
}
// Nested Array Destructuring in JS
{
let fruits = ['🍈', '🍍', '🍌', '🍉', ['🍅', '🍄', '🥕']];
const veg = fruits[4]; // returns the array ['🍅', '🍄', '🥕']
const carrot = veg[2]; // returns '🥕'
fruits[4][2]; // returns '🥕'
let [,,,,[,,carrot]] = ['🍈', '🍍', '🍌', '🍉', ['🍅', '🍄', '🥕']];
}
// How to Use the Rest Parameter in JS
{
const [tomato, mushroom, ...rest] = ['🍅', '🍄', '🥦', '🥒', '🌽', '🥕', '🥑'];
console.log(tomato); // '🍅'
console.log(mushroom); // '🍄'
console.log(rest); // ["🥦", "🥒", "🌽", "🥕", "🥑"]
}
// How to Use the Spread Operator in JS
{
const salad = ['🍅', '🍄', '🥦', '🥒', '🌽', '🥕', '🥑'];
const saladCloned = [...salad];
console.log(saladCloned); // ["🍅", "🍄", "🥦", "🥒", "🌽", "🥕", "🥑"]
salad === saladCloned // false
}
// How to Swap Values with Destructuring
{
let first = '😔';
let second = '🙂';
[first, second] = [second, first];
console.log(first); // '🙂'
console.log(second); // '😔'
}
// How to Merge Arrays
{
const emotion = ['🙂', '😔'];
const veggies = ['🥦', '🥒', '🌽', '🥕'];
const emotionalVeggies = [...emotion, ...veggies];
console.log(emotionalVeggies); // ["🙂", "😔", "🥦", "🥒", "🌽", "🥕"]
}
// Array Length Property
{
const arr1 = [11, 21, 73];
const arr2 = new Array(7);
console.log(arr1.length);
console.log(arr2.length);
arr2.length = 2 ** 32; // 4294967296
const arr3 = new Array(-10);
const arr = [11, 32];
console.log(arr);
arr.length = 5;
arr.length = 0
}
// Non-Modifiable Length Property
{
const ages = [21, 12, 73, 41, 67];
Object.defineProperty(ages, "length", { writable: false });
ages[5] = 6;
ages.push(5);
}
// The contact() method
{
const first = [1, 2, 3];
const second = [4, 5, 6];
const merged = first.concat(second);
console.log(merged); // [1, 2, 3, 4, 5, 6]
console.log(first); // [1, 2, 3]
console.log(second); // [4, 5, 6]
}
// The join() method
{
const emotions = ['🙂', '😍', '🙄', '😟'];
const joined = emotions.join();
console.log(joined); // "🙂,😍,🙄,😟"
const joined = emotions.join('<=>');
console.log(joined); // "🙂<=>😍<=>🙄<=>😟"
[].join() // returns ""
}
// The fill() method
{
const colors = ['red', 'blue', 'green'];
colors.fill('pink');
console.log(colors); // ["pink", "pink", "pink"]
const colors = ['red', 'blue', 'green'];
colors.fill('pink', 1,3); // ["red", "pink", "pink"]
}
// The includes() array method
{
const names = ['tom', 'alex', 'bob', 'john'];
names.includes('tom'); // returns true
names.includes('july'); // returns false
}
// The indexOf() array method
{
const names = ['tom', 'alex', 'bob', 'john'];
names.indexOf('alex'); // returns 1
names.indexOf('rob'); // returns -1
const names = ['tom', 'alex', 'bob', 'tom'];
names.indexOf('tom'); // returns 0
names.lastIndexOf('tom'); // returns 3
}
// The reverse() array method
{
const names = ['tom', 'alex', 'bob'];
names.reverse(); // returns ["bob", "alex", "tom"]
}
// The sort() array method
{
let artists = [
'John White Abbott',
'Leonardo da Vinci',
'Charles Aubry',
'Anna Atkins',
'Barent Avercamp'
];
let sorted = artists.sort();
console.log('Sort the artist names', sorted);
console.log(artists === sorted); // returns true
// A compare function
function (a, b) {
if (a > b) {
return -1;
}
if (a < b) {
return 1;
}
// a must be equal to b
return 0;
}
function (a,b) {
return a === b ? 0 : a > b ? -1 : 1;
}
artists.sort(function (a, b) {
return a === b ? 0 : a > b ? -1 : 1;
});
console.log('Sort the artist names(Descending)', artists);
// Sorting Numbers
let ages = [2, 1000, 10, 3, 23, 12, 30, 21];
ages.sort();
console.log(ages);
}
// The splice() array method
{
const names = ['tom', 'alex', 'bob'];
names.splice(1, 0, 'zack');
console.log(names); // ["tom", "zack", "alex", "bob"]
const names = ['tom', 'alex', 'bob'];
const deleted = names.splice(2, 1, 'zack');
console.log(deleted); // ["bob"]
console.log(names); // ["tom", "alex", "zack"]
}
// The at() method
{
const junkFoodILove = ['🥖', '🍔', '🍟', '🍕', '🌭', '🥪', '🌮', '🍿'];
junkFoodILove.at(0); // 🥖
junkFoodILove.at(3); // 🍕
junkFoodILove.at(-1); // 🍿
junkFoodILove.at(-5); // 🍕
junkFoodILove.at(-8); // 🥖
junkFoodILove.at(10); // undefined
}
// The flat() method
{
const arr1 = [0, 1, 2, [3, 4]];
console.log(arr1.flat());
const arr2 = [0, 1, [2, [3, [4, 5]]]];
console.log(arr2.flat());
console.log(arr2.flat(2));
console.log(arr2.flat(Infinity));
}
// The copyWithin() method
{
const array = [1, 2, 3, 4, 5, 6, 7];
array.copyWithin(0, 3, 6)
const array = [1, 2, 3, 4, 5, 6, 7];
array.copyWithin(0, 4)
}
// The toSorted() method
{
const months = ["Mar", "Jan", "Feb", "Dec"];
const sortedMonths = months.toSorted();
console.log(sortedMonths); // ['Dec', 'Feb', 'Jan', 'Mar']
console.log(months); // ['Mar', 'Jan', 'Feb', 'Dec']
}
// The toReversed() method
{
const items = [1, 2, 3];
console.log(items); // [1, 2, 3]
const reversedItems = items.toReversed();
console.log(reversedItems); // [3, 2, 1]
console.log(items); // [1, 2, 3]
}
// The toSpliced() method
{
const months = ["Jan", "Mar", "Apr", "May"];
const months2 = months.toSpliced(1, 0, "Feb");
}
// The with() method
{
const numbers = [1, 2, 3, 4, 5];
numbers[2] = 6;
console.log(numbers); // [1, 2, 6, 4, 5];
const newArray = numbers.with(2,6);
console.log(numbers); // Unchanged => [1, 2, 3, 4, 5];
console.log(newArray); // Changed(A new copy) => [1, 2, 6, 4, 5];
numbers[-2] = 8;
console.log(numbers); // [1, 2, 3, 4, 5, -2: 8]
const numbers = [1, 2, 3, 4, 5];
const anotherArray = numbers.with(-2, 8);
console.log(anotherArray); // [1, 2, 3, 8, 5]
anotherArray.at(-2); // 8
}
// The isArray() method
{
const arr = [1,2,3,4];
Array.isArray(arr);
Array.isArray({});
Array.isArray(1);
Array.isArray([]);
}
// The Array.of() method
{
const a = new Array(2,3,4);
const b = [4,5,6];
const c = Array.of(2, false, 'test', {'name': 'Alex'});
}
// Array Like
{
const array_like = {
0: 'A',
1: 'B',
2: 'C',
length : 3
}
array_like[1]
Array.from(array_like)
document.getElementsByTagName('li')
function checkArgs() {
console.log(arguments.length);// logs 2.
}
}
// Array.from()
{
const collection = Array.from(document.getElementsByTagName('li'))
}
// Array.fromAsync()
{
Array.fromAsync({
length: 3,
0: Promise.resolve('tapaScript'),
1: Promise.resolve('Google'),
2: Promise.resolve('Apple'),
}).then((array) => console.log(array));
}
// The Customer Array
let customers = [
{
'id': 001,
'f_name': 'Abby',
'l_name': 'Thomas',
'gender': 'M',
'married': true,
'age': 32,
'expense': 500,
'purchased': ['Shampoo', 'Toys', 'Book']
},
{
'id': 002,
'f_name': 'Jerry',
'l_name': 'Tom',
'gender': 'M',
'married': true,
'age': 64,
'expense': 100,
'purchased': ['Stick', 'Blade']
},
{
'id': 003,
'f_name': 'Dianna',
'l_name': 'Cherry',
'gender': 'F',
'married': true,
'age': 22,
'expense': 1500,
'purchased': ['Lipstik', 'Nail Polish', 'Bag', 'Book']
},
{
'id': 004,
'f_name': 'Dev',
'l_name': 'Currian',
'gender': 'M',
'married': true,
'age': 82,
'expense': 90,
'purchased': ['Book']
},
{
'id': 005,
'f_name': 'Maria',
'l_name': 'Gomes',
'gender': 'F',
'married': false,
'age': 7,
'expense': 300,
'purchased': ['Toys']
}
];
// filter() method
{
// filter example - Build Customer Data for Senior Citizens
const seniorCustomers = customers.filter((customer) => {
return (customer.age >= 60)
});
console.log('[filter] Senior Customers = ', seniorCustomers);
}
// map() method
{
// map example - Build Customer Data with title and full name
const customersWithFullName = customers.map((customer) => {
let title = '';
if(customer.gender === 'M') {
title = 'Mr.';
} else if(customer.gender === 'F' && customer.married) {
title = 'Mrs.';
} else {
title = 'Miss';
}
customer['full_name'] = title
+ " "
+ customer.f_name
+ " "
+ customer.l_name;
return customer;
});
console.log('[map] Customers With Full Name = '
, customersWithFullName);
}
// reduce() method
{
// reduce example - Get the Average Age of
// Customers who purchased 'Book'
let count = 0;
const total = customers.reduce(
(accumulator, customer, currentIndex, array) => {
if(customer.purchased.includes('Book')) {
accumulator = accumulator + customer.age;
count = count + 1;
}
return (accumulator);
},
0);
console.log('[reduce] Customer Avg age Purchased Book:'
, Math.floor(total/count));
}
// some() method
{
const hasYoungCustomer = customers.some((customer) => {
return (customer.age < 10);
});
console.log('[some] Has Young Customer(Age < 10):', hasYoungCustomer);
}
// find() method
{
const foundYoungCustomer = customers.find((customer) => {
return (customer.age < 10);
});
console.log('[find] Found Young Customer(Age < 10): ', foundYoungCustomer);
}
// findIndex() method
{
const index = customers.findIndex((customer) => {
return (customer.age < 10);
});
}
// findLastIndex() method
{
const index = customers.findLastIndex((customer) => {
return (customer.age < 10);
});
}
// findLast() method
{
const lastFoundYoungCustomer = customers.findLast((customer) => {
return (customer.age < 10);
});
console.log('[find] Last Found Young Customer(Age < 10): ', lastFoundYoungCustomer);
}
// every() method
{
const isThereWindowShopper = customers.every((customer) => {
return (customer.purchased.length === 0);
})
console.log('[every] Everyone a window shopper?', isThereWindowShopper);
}
// entries() method
{
for (const value of numbers.entries()) {
console.log(value)
}
}
// values() method
{
for (const value of numbers.values()) {
console.log(value)
}
}
// flatMap() method
{
const arr1 = [1, 2, 3, 4];
arr1.map(item => item *2);
arr1.flatMap(item => item *2);
arr1.map(item => [item *2]);
arr1.flatMap(item => [item *2]);
arr1.map(item => [[item *2]]);
arr1.flatMap(item => [[item *2]])
}
// reduceRight() method
{
let number = [100, 40, 15];
number.reduceRight((accumulator, current) => {
return accumulator - current
});
}
// Array method Chaining
{
const marriedCustomers = customers.filter((customer) => {
return (customer.married);
});
const expenseMapped = marriedCustomers.map((marriedCustomer) => {
return marriedCustomer.expense;
});
const totalExpenseMarriedCustomer = expenseMapped.reduce(
(accum, expense) => {
return accum + expense;
}, 0);
console.log('Total Expense of Married Customers in INR: '
, totalExpenseMarriedCustomer);
// After Chining them
const total = customers
.filter(customer => customer.married)
.map(married => married.expense)
.reduce((accum,expense) => accum + expense);
console.log('Orchestrated total expense in INR: ', total);
}