-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayInJS.js
246 lines (193 loc) · 5.24 KB
/
ArrayInJS.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
// Traversal of array.
var myFriends = ['Kul', 'deep', 11, 58.25, false];
// This is traditional for loop.
for (var i = 0; i < myFriends.length; i++) {
console.log(myFriends[i]);
}
var arrMyFriends = new Array;
arrMyFriends.push(12);
arrMyFriends.push(13);
arrMyFriends.push('Kul');
arrMyFriends.push('Singh');
// push methods adds the value at the end and returns the length of newly formed array.
var y = arrMyFriends.push(89.96);
console.log();
console.log(y);
console.log(arrMyFriends.length);
for (var i = 0; i < arrMyFriends.length; i++) {
console.log(arrMyFriends[i]);
}
console.log();
arrMyFriends.pop();//Remove the last elements and returns the popped element.
for (var i = 0; i < arrMyFriends.length; i++) {
console.log(arrMyFriends[i]);
}
console.log();
// For in loop and For of loop
for (var key in arrMyFriends) {
console.log(key + " " + arrMyFriends[key]);
}
console.log();
for (var key of arrMyFriends) {
console.log(key);
}
// For each loop
arrMyFriends.forEach(function (element, index, array) {
console.log(element + " : " + index + " : " + array);
});
console.log();
arrMyFriends.forEach((element, index, array) => {
console.log(element + " : " + index + " : " + array);
});
/**
*
D:\DailyStuff\JavaScript>node ArrayInJS.js
Kul
deep
11
58.25
false
5
12
13
Kul
Singh
89.96
12
13
Kul
Singh
0 12
1 13
2 Kul
3 Singh
12
13
Kul
Singh
12 : 0 : 12,13,Kul,Singh
13 : 1 : 12,13,Kul,Singh
Kul : 2 : 12,13,Kul,Singh
Singh : 3 : 12,13,Kul,Singh
12 : 0 : 12,13,Kul,Singh
13 : 1 : 12,13,Kul,Singh
Kul : 2 : 12,13,Kul,Singh
Singh : 3 : 12,13,Kul,Singh
*
*/
// Searching and filter in array
var array = [12, 45, 90, 90, 100, 101];
console.log(array.indexOf(100)); //4
console.log(array.indexOf(4)); //-1
// Returns the index of the first occurrence of a value in an array, or -1 if it is not present.
console.log(array.lastIndexOf(90)); // 3
// Returns the index of the last occurrence of a specified value in an array, or -1 if it is not present.
array.push("Kul", 'BP', 'Singh');
console.log(array);
/**
* [
12, 45,
90, 90,
100, 101,
'Kul', 'BP',
'Singh'
]
*/
// Unshist() methods - Inserts new elements at the start of an array, and returns the new length of the array.
array.unshift("In starting");
console.log(array); //[ 'In starting', 12, 45, 90, 90, 100, 101, 'Kul', 'BP', 'Singh' ]
// 2nd Example
const myNumbers = [1, 2, 3, 5];
myNumbers.unshift(4, 6);
console.log(myNumbers) // [4, 6, 1, 2, 3, 5 ]
// Sorting in array
var mySort = [34, 1, 90, 56, 0, 54];
mySort.sort();
console.log(mySort); // [ 0, 1, 34, 54, 56, 90 ]
/**
* push() method add the data in end while
* unshift() method add the data in the starting
* pop() removes the last element and return its
* whereas shift() method removes the first element and
* returns it
* */
var x = myNumbers.shift();
console.log(x); // 4
// How to sort the numbers in the array in ascending(up) and descending(down) order.
const myUpAndDown = [78, 0, 23, 1, 7, 34, 16, 13];
myUpAndDown.sort((a, b) => { return a - b });
console.log(myUpAndDown);
myUpAndDown.sort((a, b) => { return b - a });
console.log(myUpAndDown);
/*
[
0, 1, 7, 13,
16, 23, 34, 78
]
[
78, 34, 23, 16,
13, 7, 1, 0
]
*/
// Splice() method
const month = ['J', 'F', 'M', 'A', 'J', 'Ju'];
// month.splice(6, 0, 'D');
// or
month.splice(month.length, 0, 'D');
console.log(month); //['J', 'F', 'M', 'A', 'J', 'Ju', 'D'];
month.splice(2, 3, 'Au'); //['J', 'F','Au', 'Ju', 'D'];
console.log(month);
// Filter() method in javascript
function func() {
var toBeFilter = [12, 45, 21, 90, 4, 5].filter((a) => { return a >= 18; });
console.log(toBeFilter); // 45, 21, 90.
}
func();
// Map() method in JS
const array1 = [1, 4, 9, 16, 25];
let newArr = array1.map((cuurElememt, index, arr) => {
return cuurElememt>9;
// if(cuurElememt > 9){
// return cuurElememt;
// }else{
// return "";
// }
});
console.log(newArr); // [ false, false, false, true, true ]
//Find the square root of each element in the array
let arr = [25, 36, 49, 64, 81];
let modifiedArr = arr.map((curr, index, arr) => {
return Math.sqrt(curr);
})
console.log(modifiedArr); // [ 5, 6, 7, 8, 9 ]
// Multiply each number by 2 and return only those which are greater then 10;
let numberN2 = [2, 3, 4, 6, 8];
/*
let newNumberN2 = numberN2.map((curr, index, array) => {
return curr*2;
}).filter((curr) => {
return curr > 10;
});
console.log(newNumberN2); // [ 12, 16 ]
*/
let newNumberN2 = numberN2.map( (curr) => curr*2 ).filter((curr) => curr > 10);
console.log(newNumberN2); // [ 12, 16 ]
// Reduce() in JS
// Add the elements of thet array.
let arr1 = [5, 6, 2];
let sum = arr1.reduce((accumulator, curr) => {
// debugger;
return accumulator += curr;
});
console.log(sum); // 13
// How to flatten an array
const arr2 = [
[1, 2, 3],
[4, 5, 6],
[7,8,9]
]
let flatArr = arr2.reduce((accum, currVal) => {
return accum.concat(currVal);
});
console.log(flatArr); // [ 1, 2, 3, 4, 5,6, 7, 8, 9 ]
console.log(arr2.flat());// [ 1, 2, 3, 4, 5,6, 7, 8, 9 ]