-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrays.js
More file actions
259 lines (173 loc) · 6.31 KB
/
Arrays.js
File metadata and controls
259 lines (173 loc) · 6.31 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
let Name=["Nikhil" , "Karan" ,"Rohit" , "Raj" , "Ram" , "Part" ]
//console.log(Name[2] , "\n");
let Number=[1,2,3,4,5,6,4,7,8,9,]
// for (const item in Name) {
// if (Object.prototype.hasOwnProperty.call(Name, item)) {
// console.log( Name[item]);
// }
// }
// Name.forEach((element , index , arr) => {
// console.log(`${element} , ${index} `);
// console.log(`${arr}`);
// });
// Name.map((element , index , arr)=>{
// console.log(element, index);
// console.log(arr);
// })
// Number.forEach((element , index , arr) => {
// console.log(element * 2);
// });
// Number.map((element , index ,arr )=>{
// console.log(element * 2);
// })
//Add A new element in Array (last)
// Name.push("Jay")
// console.log(Name);
//Remove a last element in Array(last)
// Name.pop("karan")
// console.log(Name);
//Add A New element in Array(staring)
// Name.unshift("Deep")
// console.log(Name);
//remove A element in Array(staring)
// Name.shift("Deep")
// console.log(Name);
//array.splice(start, deleteCount, item1, item2, );
// Name.splice(2,2,"Jigr")
// console.log(Name);
//indexOf is searchElement Search Direction Left ➝ Right
//console.log(Number.indexOf(4));
//lastindexOf Search Direction Right ➝ Left
// console.log(Number.lastIndexOf(4));
// const months = ['Jan', 'march', 'April', 'June', 'July'];
// months.push("Des")
// console.log(months);
// console.log(months.splice());
// console.log(months.splice(1, 1, "March"));
// console.log(months);
//filter
// let numbers= [1,2,3,4,5,6,7,8,9]
//find
// numbers.find((curtnelement)=>{
// if (curtnelement > 6) {
// var value = curtnelement
// console.log(value);
// }
// })
// numbers.findIndex((curtnele)=>{
// console.log(curtnele);
// })
// const products = [
// { name: "Laptop", price: 1200 },
// { name: "Phone", price: 800 },
// { name: "Tablet", price: 300 },
// { name: "Smartwatch", price: 150 },
// ];
// products.filter((item)=>{
// console.log(item.name , "=" , item.price);
// })
// let nums = [1,3,5,4,3,2,9,7,8,5,3,1]
// nums.sort((a, b) => a - b);
// console.log(nums);
// Name.sort()
// console.log(Name);
// AI Expline
// ========================
// JavaScript Array Methods
// ========================
// 1. push() -> Add element at END
let arr1 = [1, 2];
arr1.push(3);
console.log(arr1); // [1, 2, 3]
// 2. pop() -> Remove element from END
let arr2 = [1, 2, 3];
arr2.pop();
console.log(arr2); // [1, 2]
// 3. shift() -> Remove element from START
let arr3 = [1, 2, 3];
arr3.shift();
console.log(arr3); // [2, 3]
// 4. unshift() -> Add element at START
let arr4 = [2, 3];
arr4.unshift(1);
console.log(arr4); // [1, 2, 3]
// 5. splice() -> Add/Remove elements at any position
let arr5 = [1, 2, 3, 4];
arr5.splice(1, 2, 9, 8); // (index=1, delete 2, insert 9,8)
console.log(arr5); // [1, 9, 8, 4]
// 6. slice() -> Copy portion of array
let arr6 = [10, 20, 30, 40];
console.log(arr6.slice(1, 3)); // [20, 30]
// 7. indexOf() -> First index of element
console.log([10, 20, 30].indexOf(20)); // 1
// 8. lastIndexOf() -> Last index of element
console.log([1, 2, 3, 2].lastIndexOf(2)); // 3
// 9. includes() -> Check if element exists
console.log([1, 2, 3].includes(2)); // true
// 10. find() -> First matching element
console.log([5, 12, 8, 130].find(x => x > 10)); // 12
// 11. findIndex() -> Index of first matching element
console.log([5, 12, 8, 130].findIndex(x => x > 10)); // 1
// 12. findLast() -> Last matching element (ES2023)
console.log([5, 12, 8, 130].findLast(x => x > 10)); // 130
// 13. findLastIndex() -> Index of last matching element
console.log([5, 12, 8, 130].findLastIndex(x => x > 10)); // 3
// 14. forEach() -> Run function on each element (no return)
[1, 2, 3].forEach(x => console.log(x * 2)); // 2, 4, 6
// 15. map() -> Transform each element into new array
console.log([1, 2, 3].map(x => x * x)); // [1, 4, 9]
// 16. filter() -> Get elements that satisfy condition
console.log([1, 2, 3, 4].filter(x => x % 2 === 0)); // [2, 4]
// 17. reduce() -> Reduce array to single value
console.log([1, 2, 3, 4].reduce((a, b) => a + b, 0)); // 10
// 18. reduceRight() -> Same as reduce, but from right side
console.log(["a", "b", "c"].reduceRight((a, b) => a + b)); // "cba"
// 19. some() -> True if ANY element satisfies condition
console.log([1, 2, 3].some(x => x > 2)); // true
// 20. every() -> True if ALL elements satisfy condition
console.log([2, 4, 6].every(x => x % 2 === 0)); // true
// 21. sort() -> Sort array (default string, use callback for numbers)
let arr21 = [3, 1, 4, 2];
arr21.sort((a, b) => a - b);
console.log(arr21); // [1, 2, 3, 4]
// 22. reverse() -> Reverse the array
let arr22 = [1, 2, 3];
arr22.reverse();
console.log(arr22); // [3, 2, 1]
// 23. toSorted() -> Sorted copy (original unchanged) ES2023
let arr23 = [3, 1, 2];
console.log(arr23.toSorted((a, b) => a - b)); // [1, 2, 3]
console.log(arr23); // [3, 1, 2]
// 24. toReversed() -> Reversed copy (immutable) ES2023
let arr24 = [1, 2, 3];
console.log(arr24.toReversed()); // [3, 2, 1]
console.log(arr24); // [1, 2, 3]
// 25. join() -> Convert array to string
console.log(["a", "b", "c"].join("-")); // "a-b-c"
// 26. toString() -> Convert to string (comma separated)
console.log([1, 2, 3].toString()); // "1,2,3"
// 27. toLocaleString() -> Locale-specific string
console.log([1000, new Date()].toLocaleString());
// 28. flat() -> Flatten nested arrays
console.log([1, [2, [3, 4]]].flat(2)); // [1, 2, 3, 4]
// 29. flatMap() -> map() + flat() together
console.log([1, 2, 3].flatMap(x => [x, x * 2])); // [1, 2, 2, 4, 3, 6]
// 30. fill() -> Replace part of array with value
let arr30 = [1, 2, 3, 4];
arr30.fill(0, 1, 3);
console.log(arr30); // [1, 0, 0, 4]
// 31. copyWithin() -> Copy elements inside array
let arr31 = [1, 2, 3, 4, 5];
arr31.copyWithin(0, 3);
console.log(arr31); // [4, 5, 3, 4, 5]
// 32. keys() -> Iterator of indexes
for (let k of ["a", "b"].keys()) console.log(k); // 0, 1
// 33. values() -> Iterator of values
for (let v of ["a", "b"].values()) console.log(v); // a, b
// 34. entries() -> Iterator of [index, value]
for (let [i, v ] of ["a", "b"].entries()) console.log(i, v); // 0 a, 1 b
// 35. with() -> Immutable element replace (ES2024)
let arr35 = [1, 2, 3];
let newArr = arr35.with(1, 9);
console.log(newArr); // [1, 9, 3]
console.log(arr35); // [1, 2, 3]