forked from Swap76/Learn-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_methods.js
More file actions
232 lines (203 loc) · 7.09 KB
/
array_methods.js
File metadata and controls
232 lines (203 loc) · 7.09 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
220
221
222
223
224
225
226
227
228
229
230
231
232
// 1. join() method
const sayings = ["India", "is", "my", "country"];
sayings.join(" - ");
//Output : India-is-my-country
// 2. concat() method
var flowers = ["Rose", "Lotus"];
var leaf = ["green", "lightgreen", "Darkgreen"];
var garden = flowers.concat(leaf);
//Output : Rose ,Lotus,green,lightgreen Darkgreen
// 3. copyWithin() method
var Country = ["India", "US", "German", "Australia"];
console.log(Country); // Output : India , Us , German , Australia
Country.copyWithin(2, 0); // Output : India , Us , India , Us
// 4.fill() method
var Country = ["India", "US", "German", "Australia"];
console.log(Country); // Output : India , Us , German , Australia
Country.fill("Canada"); // Output : Canada, Canada , Canada, Canada
// 5. find() method
var years = [2000, 2001, 2002, 2003];
console.log(years); // Output : 2000,2001,2002,2003
function checkYear(year) {
return year >= 2002;
}
years.find(checkYear); // Output: 2002
// 6. findIndex()method
var years = [2000, 2001, 2002, 2003];
console.log(years); // Output : 2000,2001,2002,2003
function checkYear(year) {
return year >= 2002;
}
years.findIndex(checkYear); // Output: 2
// 7.forEach() method
var Country = ["India", "US", "German", "Australia"];
console.log(Country); // Output : India , Us , German , Australia
Country.forEach(countryFunction);
function countryFunction(item) {
console.log(item) // Output: India , Us , German , Australia
}
// 8. isArray() method
var Country = ["India", "US", "German", "Australia"];
console.log(Country); // Output : India , Us , German , Australia
Array.isArray(Country); //Output: true
// 9.includes() method
var Country = ["India", "US", "German", "Australia"];
console.log(Country); // Output : India , Us , German , Australia
var avail = Country.includes("Australia");
console.log(avail); // Output : true
// 10. IndexOf() method
var Country = ["India", "US", "German", "Australia"];
console.log(Country); // Output : India , Us , German , Australia
var iof = Country.indexOf("Australia");
console.log(iof); // Output : 3
// 11 map() method
var numbers = [1, 2, 3, 4, 5];
console.log(numbers); //[1,2,3,4,5]
function double(num) {
return num * 2;
}
numbers = numbers.map(double);
console.log(numbers); //[2,4,6,8,10]
{
// 12 Array.prototype.reduce() Method
// This method is used to generate a single output from the array
let toReduceArray = [1, 2, 3, 4, 5];
console.log("\nArray that will implement .reduce()")
console.log(toReduceArray);
function callbackFunctionForReduce(accumulator, currentValue) {
return accumulator + currentValue.toString();
}
let reducedArray = toReduceArray.reduce(callbackFunctionForReduce);
console.log("Reduced array is: ");
console.log(reducedArray);
}
{
// 13 Array.prototype.reduceRight() Method
// This method is used to generate a single output from array by processing it from right to left
let toReduceArray = [1, 2, 3, 4, 5];
console.log("\nArray that will implement .reduceRight()")
console.log(toReduceArray);
function callbackFunctionForReduceRight(accumulator, currentValue) {
return accumulator + currentValue.toString();
}
let reducedArray = toReduceArray.reduceRight(callbackFunctionForReduceRight);
console.log("Reduced array is: ");
console.log(reducedArray);
}
{
//14 Array.prototype.some()
// Checks whether at least one element in the array passes the test given as a parameter
let toCheckArray = [1, 2, 3, 4, 5];
console.log('Array to be checked for containing a multiple of 2 is:');
console.log(toCheckArray);
function checkingFunction(input) {
// Checks if the value is multiple of 2;
return ((input % 2) === 0);
}
console.log('Result is: ');
console.log(toCheckArray.some(checkingFunction));
}
{
//15 Array.prototype.lastIndexOf()
// Finds the first element supplied as parameter from the last posistion in the array
let containingArray = [1, 7, 6, 8, 7];
console.log("the array that contains 7 two times in position 1,4 is: ");
console.log(containingArray);
console.log('The last posistion of 7 in array according to lastIndexOf() is: ');
console.log(containingArray.lastIndexOf(7));
}
{
//16 Array.prototype.every()
//Checks if all the elements in array passes the condition implemented by the function given as parameter
let fullfillingArray = [2, 4, 6, 8, 10];
console.log('Array with all even numbers is: ');
console.log(fullfillingArray);
function checkIfEven(num) {
return ((num % 2) === 0);
}
console.log('Check if all elements in array are even:');
console.log(fullfillingArray.every(checkIfEven));
}
{
//17 Array.prototype.filter()
//returns all the elements in array that pass the test implemented by function passed as parameter
let evenOddArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log('the array from which to return all even numbers is: ');
console.log(evenOddArray);
console.log('Filetered array is: ');
console.log(evenOddArray.filter(checkIfEven));
}
{
//18 Array.prototype.flat()
// This function returns the array with the consitituents array elements
let arrayToFlatten = [
[1, 3, 5, 7, 9],
[2, 4, 6, 8]
];
console.log('Array to flatten is: ');
console.log(arrayToFlatten);
console.log('Flattened array is: ');
console.log(arrayToFlatten.flat());
// an index is passed into the method that instructs the depth till which to flatten
arrayToFlatten = [
[
[1],
[3],
[5],
[7],
[9]
],
[
[2],
[4],
[6],
[8]
]
];
console.log("Deepended array is: ");
console.log(arrayToFlatten);
console.log('Array flattened to depth of 1 is: ');
console.log(arrayToFlatten.flat(1));
}
{
//19 Array.prototype.shift()
// this functions removes the element from index 0 and returns it. It is similar to pop but it removes from left
let originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log('Original array is:');
console.log(originalArray);
console.log('The element removed after shift is: ', originalArray.shift());
console.log('Array after shifting is:');
console.log(originalArray);
}
{
//20 Array.prototype.unshift()
// this functions adds the element to index 0 and returns array. It is similar to push but it adds to left
let originalArray = [3, 4, 5, 6, 7, 8, 9];
console.log('Original array is:');
console.log(originalArray);
console.log('Elements to be added in front are 1,2');
originalArray.unshift(1, 2);
console.log('Array after unshifting is:');
console.log(originalArray);
}
{
//21 Array.prototype.push();
// this function adds elements to the end of the array;
let originalArray = [1, 2, 3, 4, 5, 6, 7];
console.log('Original array is:');
console.log(originalArray);
console.log('Elements to be pushed are: 8,9');
originalArray.push(8, 9);
console.log('Array after push is: ');
console.log(originalArray);
}
{
//22 Array.prototype.pop();
// this function removes elements from the end of array;
let originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1];
console.log('Original array is:');
console.log(originalArray);
console.log('The element removed after pop is: ', originalArray.pop());
console.log('Array after pop is:');
console.log(originalArray);
}