-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrays.js
195 lines (150 loc) · 5.18 KB
/
arrays.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
// Arrays in javascript : collection of items
// Arrays is an special type of object
// Create Array
// An array is a special variable, which can hold more than one value:
// const cars = ["Saab", "Volvo", "BMW"];
// Example of syntax :
// let marks = [97, 28, 37, 64, 57];
// console.log(marks);
// Output will be more define as position with length :
// (5) [97, 28, 37, 64, 57]
// Example with storing heroes names :
// let heroes = ["spiderman", "ironman", "thor", "hulk", "shaktiman",];
// console.log(heroes);
// Example with finding the length of arrays :
// let marks = [22, 29, 256, 54, 46 ];
// console.log(marks.length); //Property
// Output will be : 5
// console.log(typeof(marks));
// Output will be : 'object'
// Or in console we can find that type of array marks in an object : 'object'
// Array Indices : arr[0], arr[1], arr[2],.....
// An array is a special variable, which can hold more than one value:
// const cars = ["Saab", "Volvo", "BMW"];
// cars[0] in console
// Output will be : 0th term will be : Saab
// Arrays can be changed but strings cannot be changed as arrays are mutable & strings aren't mutable
// We can find the value of array via its position :
// Example 1 :
// const marks = [29, 78, 63, 33, 47];
// console.log(marks[0]); // as this function can describe the 0th term of array 'marks'
// Output will be : 29
// Example 2 :
// console.log(marks[1]);
// Output will be : 78
// Looping over an array :
// Print all elements of an array
// let heroes = ["spiderman", "ironman", "thor", "hulk", "shaktiman"];
// Usually :
// console.log(heroes[0]);
// console.log(heroes[1]);
// console.log(heroes[2]);
// console.log(heroes[3]);
// Output :
// arrays.js:51 spiderman
// arrays.js:51 ironman
// arrays.js:52 thor
// arrays.js:53 hulk
// Better pratice are loops : loops in Array
// Array length = last index + 1
// Example 1 : for printing all elements in an array:
// let arr = [1, 2, 3, 4, 5];
// for loop
// for(let index = 0; index<arr.length ; index++){
// console.log(arr[index]);
// }
// Output will be :
// 1
// 2
// 3
// 4
// 5
// For of loop
// for(let num of arr){
// console.log(num);
// }
// Output
// 1
// 2
// 3
// 4
// 5
// Example 1 :
// let cities = ["delhi", "kolkata", "luckhnow", "mumbai", "pune"];
// for (let city of cities){
// console.log(city);
// }
//Output will be : delhi
// arrays.js:92 kolkata
// arrays.js:92 luckhnow
// arrays.js:92 mumbai
// arrays.js:92 pune
// Example 2 :
// let cities = ["delhi", "kolkata", "luckhnow", "mumbai", "pune"];
// for (let city of cities){
// console.log(city.toUpperCase());
// }
// Output will be :
// DELHI
// KOLKATA
// LUCKHNOW
// MUMBAI
// PUNE
// Arrays Methods
// Push() : add to end
// pop(): delete from end & return
// to string() : converts array to string
// Example 1 Push() : add to end :
// let foodItems = ["potato", "apple", "litchi", "tomato"];
// foodItems.push("chips");
// console.log(foodItems);
// Output will be : (5) ['potato', 'apple', 'litchi', 'tomato', 'chips']
// Example 2 pop(): delete from end & return :
// foodItems.pop()
// console.log(foodItems);
//(4) ['potato', 'apple', 'litchi', 'tomato']
// Or
// let fastfood = ["burger", "pasta", "coke", "french fries"];
// fastfood.pop();
// console.log(fastfood);
//(3) ['burger', 'pasta', 'coke']
// Or log the deleted items :
// let deletedItems = fastfood.pop();
// console.log(deletedItems);
// Output will be : french fries
// Example 3 to string() : converts array to string :
// let itemstoString = fastfood.toString();
// console.log(itemstoString);
// Output will be : burger,pasta,coke
// Array Methods :
// Concat Method
// let marvel_heroes = ["thor", "spiderman", "ironman"];
// let dc_heroes = ["superman", "batman"];
// let inian_Heroes = ["Shaktiman", "Balveer"];
// let allheroes = marvel_heroes.concat(dc_heroes);
// let heroes = marvel_heroes.concat(dc_heroes,inian_Heroes);
// Output will be :
// console.log(heroes);
// Output will be : (5) ['thor', 'spiderman', 'ironman', 'superman', 'batman']
// console.log(allheroes);
// Output will be : (7) ['thor', 'spiderman', 'ironman', 'superman', 'batman', 'Shaktiman', 'Balveer']
// Unshift Method : its add arrays value on start
// marvel_heroes.unshift("antman");
// & similarly shift : to delete the value
// let marvel_heroes = ["thor", "spiderman", "ironman"];
// let value = marvel_heroes.shift("thor");
// console.log("deleted" ,value,marvel_heroes);
// Output will be : (2) ['spiderman', 'ironman']
// slice method :
// let dc_heroes = ["superman", "batman"];
// let slicevalue = dc_heroes.slice();
// console.log(slicevalue);
// slice method with excluded terms :
// console.log(slicevalue.slice(0,1));
// Output will be from excluding 1st term : ['superman'] 0th term
// Splice() method : change original array (go to index, remove, replace)
// let arr = ["fuchka", "egg roll", "chaomin", "ice cream", "coke", "cake" ];
// let arrsplice = arr;
// console.log("Full Term = ",arrsplice,"final term will be : ", arr.splice(2,2,"mouthfreshner", "silver pearls"));
// // Output will be : final term will be : (2) ['chaomin', 'ice cream']
// console.log(arr); // Output will be : (6) ['fuchka', 'egg roll', 'mouthfreshner', 'silver pearls', 'coke', 'cake']