-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrays.js
More file actions
214 lines (137 loc) · 4.59 KB
/
Arrays.js
File metadata and controls
214 lines (137 loc) · 4.59 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
// Array is collection of item with different data types like , string , boolean , integers , objects
// Array are variable which can hold more than one values
// array is mutable array can be change
const fruit = ["Banana", "Apple", "Orange"]
const a1 = [7, "Saad", false] // also store different values
console.log(a1)
// accessing the values
let numbers = [1, 2, 3, 4, 5]
console.log(numbers[0])
console.log(numbers[2])
console.log(numbers[4])
console.log(numbers[8])
// finding the length
console.log(numbers.length)
// changing the values
numbers[4] = 8
console.log(numbers) // [ 1,2,3,4,8]
// type of array is object
console.log(typeof numbers) //object
const arr = [1, , 3, "Saad", " Coding", true, null, undefined, 4.3,]
for (let i = 0; i < arr.length; i++) {
console.log(arr[i])
}
for (let i in arr) {
console.log(arr[i])
}
for (let i of arr) {
console.log(arr[i])
}
// array methods in js
// array form
let name = "Saad Ali"
let a = Array.from(name);
console.log(a)
let names = ["Saad" , "Ali" , "Hassan"]
console.log(names)
// check the difference
// 1. push()
names.push("Abrar")
console.log(names)
// 2. pop()
console.log(names.pop())
console.log(names)
// 3. toString()
let num = [12345, 45667]
console.log(num.toString())
console.log(names.toString()) // return normal string to proper array
// 4. concat array
let arr1= [1,2,3,4,5]
let arr2= [6,7,8,9,10]
console.log(arr1.concat(arr2)) // they does not affect original array
// 5. unshift() they work like push but reverse
names.unshift("Abrar" , 1)
console.log(names)
// 6. shift() they work like pop but reverse
names.shift()
console.log(names)
// 7. slice() they does not affect the original array
console.log(names.slice(1,3)) // the last index is not include in slice
// 8. splice they change original array
//splice( stratIndex, endIndex ,newItem)
console.log("Original array:");
console.log(names);
let change = names.splice(1, 1, "Haroon");
console.log("\nRemoved element(s):");
console.log(change);
// The original array after the modification
console.log("\nModified array:");
console.log(names);
// 7. forEach()
let no = [3,6,1,8,2,9]
console.log("Using forEach():");
no.forEach(function(no) {
console.log(no * 2); // Multiply each number by 2
});
// 8. map() create new array by pefroming some operation on each array element
let doubled = no.map(function(no) {
return no * 2;
});
console.log("Using map():");
console.log(doubled);
// 9. filter() filter an array with values that passes the test creates a new array
let filtered = no.filter(function(no) {
return no > 3;
});
console.log("Using filter():");
console.log(filtered);
// 12. reduce() reduce function reduce the array and return the singlr value
// syntax : array.reduce(callback(accumulator, currentValue, currentIndex, array), initialValue)
let number = [1, 2, 3, 4, 5];
let sum = number.reduce(function(accumulator, currentValue) {
return accumulator + currentValue;
}, 0); // 0 is the initial value for the accumulator
console.log("Sum of numbers:");
console.log(sum); // Output: 15
let product = number.reduce(function(accumulator, currentValue) {
return accumulator * currentValue;
}, 1); // 1 is the initial value for multiplication
console.log("Product of numbers:");
console.log(product); // Output: 24
let arrays = [[1, 2], [3, 4], [5, 6]];
let flattened = arrays.reduce(function(accumulator, currentValue) {
return accumulator.concat(currentValue);
}, []);
console.log("Flattened array:");
console.log(flattened); // Output: [1, 2, 3, 4, 5, 6]
let fruites = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple'];
let count = fruites.reduce(function(accumulator, fruit) {
if (!accumulator[fruit]) {
accumulator[fruit] = 1; // Initialize the count if it doesn't exist
} else {
accumulator[fruit]++; // Increment the count if it exists
}
return accumulator;
}, {});
console.log("Fruit count:");
console.log(count); // Output: { apple: 3, banana: 2, orange: 1 }
// 11 . sort()
let fruits = ["Banana", "Orange", "Apple", "Mango"];
let sortedFruits = fruits.sort();
console.log("Using sort():");
console.log(sortedFruits); // ["Apple", "Banana", "Mango", "Orange"]
// // Sorting numbers with a compare function
let sortedNumbers = no.sort(function(a, b) {
return a - b; // Sort numbers in ascending order
});
console.log("Sorted numbers:");
console.log(sortedNumbers);
// extra things
// we can use the variable for function also use has a variable but not when we use const
let b = function()
{
return "Saad"
}
console.log(b());
b = 12;
console.log(b)