-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJS_cheat-sheet.txt
More file actions
129 lines (86 loc) · 4.9 KB
/
JS_cheat-sheet.txt
File metadata and controls
129 lines (86 loc) · 4.9 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
* Javascript is based on ECMAscript specification
* which is standard set by "European Computer Manifacturer Associations" (ECMA)
* 'var' and 'let' are both used for variable declaration in javascript but the difference between them is that 'var' is function scoped and 'let' is block scoped.
* To get number of milliseconds from 1970
new Date().getTime(); (OR) +new Date();
* To get number of seconds from 1970
Math.round(new Date().getTime()/1000);
* To get 9 months after date & seconds
let today = new Date();
today.setMonth(9);
console.log("+9 month date --> " + new Date(today));
console.log("+9 months Timestamp --> " + Math.round(new Date(today).getTime() / 1000));
* Diff B/W normal func & Arrow func is
Arrow func don't have their own context.
*********************************** Examples *********************************************
--> let numbersArray = [1,2,3,4,5,6,7,8,9];
--> let fruitsArray = ["Banana", "Orange", "Apple", "Mango"];
--> let sampleArray = ['I', 'am', 'a', 'Software', 'Developer'];
--> let sampleArrayOfObjects = [
{prop1:"abc",prop2:"qwe"},
{prop1:"bnmb",prop2:"yutu"},
{prop1:"zxvz",prop2:"qwrq"}
];
--> let stringVar = 'Professional';
--> let sampleString = 'I am a Software Developer';
--> let string = "Select~Nil~1+~2+~3+";
* To make first character upper case then
-> stringVar.charAt(0).toUpperCase() + stringVar.slice(1);
* Creating an array from a string
let createdArray = sampleString.split(" ");
* Creating a string from an array
let createdString = sampleArray.join(' ');
* push() method adds new items to the end of an array, and returns the new length.
let newArray = sampleArray.push('Manoj');
* unshift() method adds new items to the beginning of an array, and returns the new length.
let newArray = sampleArray.unshift('Yayyy');
* pop() method is used to remove last item from an array and return the removed item
let removedItem = sampleArray.pop();
* shift() method is used to remove first item from an array and return the removed item
let removedItem = sampleArray.shift();
* splice() method can be used to add new items to an array
Ex: fruitsArray.splice(2, 0, "Lemon", "Kiwi");
-> The first parameter (2) defines the position where new elements should be added (spliced in).
-> The second parameter (0) defines how many elements should be removed.
-> The rest of the parameters ("Lemon" , "Kiwi") define the new elements to be added.
-> The splice() method returns an array with the deleted items
For the above example result will be
New Array: Banana,Orange,Lemon,Kiwi
Removed Items: Apple,Mango
* Adding extra properties to an array of object using higher order method 'map'
-> this.freshDispenses = this.consumableDispenses = dispenses.map(
dispense => ({
...dispense,
allowQuantity: false,
quantity: null
})
);
* 'split' & 'filter' functions combined
-> let filteredArray = string.split("~")).filter(l => l != 'Select');
* Finding index from an array
-> let index = sampleArray.indexOf('Software'); // Output is 3
* Finding index from an array of objects
-> let index = sampleArrayOfObjects.findIndex(x => x.prop2 ==="yutu"); // Output is 2
* Converting a date to timestamp
let createdAt = "2020-02-12 12:39:30".split(' ');
let createdDate = createdAt[0];
let splitDate = createdDate.split("-");
let createdTimestamp = splitDate[1] + "/" + splitDate[2] + "/" + splitDate[0];
console.log("createdTimestamp in MS --> " + new Date(createdTimestamp).getTime());
*********************************** IMP points *******************************************
* 'Increament' & 'Decreament' operators change the original value.
******************************************************************************************
*********************************** Patterns *********************************************
*** https://www.tutorialstonight.com/js/javascript-star-pattern.php
*** https://www.tutorialstonight.com/number-pattern-programs-in-javascript.php
******************************************************************************************
*********************************** Useful links *****************************************
* https://stackoverflow.com/questions/7295843/allow-only-numbers-to-be-typed-in-a-textbox
* get week number of month from date --> https://codepen.io/bhaskars11/pen/GNQOwM
* https://stackoverflow.com/questions/9873197/how-to-convert-date-to-timestamp
* https://stackoverflow.com/questions/5767325/how-do-i-remove-a-particular-element-from-an-array-in-javascript
* https://www.w3schools.com/js/js_array_sort.asp
* https://stackoverflow.com/questions/15595652/focus-next-input-once-reaching-maxlength-value
* https://www.youtube.com/watch?v=tiRhFGnCltw
******************************************************************************************
*********************************** Next *************************************************