Skip to content

Commit 8c34283

Browse files
feat: add updated code of array.js file
1 parent 1966515 commit 8c34283

File tree

1 file changed

+51
-77
lines changed

1 file changed

+51
-77
lines changed

part4 (Arrays and Loops)/01_array.js

Lines changed: 51 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ console.log(fruitsCollection);
4040

4141
// how to delete a value from array
4242
console.log(fruitsCollection.pop());
43+
console.log(fruitsCollection.shift());
4344
console.log(fruitsCollection);
4445

4546
// Map
@@ -62,6 +63,9 @@ numbers.forEach((num) => console.log(num));
6263
const found = numbers.find((num) => num > 1);
6364
console.log(found);
6465

66+
const numberIndex = numbers.findIndex((num) => num > 2);
67+
console.log(numberIndex);
68+
6569
// every / some
6670
console.log(numbers.every((num) => num > 0)); // true
6771
console.log(numbers.some((num) => num > 0)); // true
@@ -74,7 +78,7 @@ console.log(combined); // [1, 2, 3, 4, 5];
7478
// slice
7579
console.log(numbers.slice(0, 2)); // [1, 2]
7680

77-
// splce
81+
// splice
7882
numbers.splice(1, 1, 10);
7983
console.log(numbers); // [1, 10, 3]
8084

@@ -117,65 +121,23 @@ console.log(name); // Alice
117121
console.log(hobby1); // Reading
118122
console.log(hobby2); // Gaming
119123

120-
// INFO: Advanced Array Things
121-
122-
// 🔥 Main Points About Each Method
123-
124-
const arrayMethods = [
125-
{
126-
Method: "Array.from()",
127-
WhatItDoes: "Converts array-like objects or iterables to real arrays.",
128-
WhyItsUseful: "Allows use of array methods (.map(), .filter(), etc.).",
129-
},
130-
{
131-
Method: "Array.of()",
132-
WhatItDoes: "Creates an array from arguments.",
133-
WhyItsUseful: "Unlike Array(), it treats numbers as values, not length.",
134-
},
135-
{
136-
Method: "Array.isArray()",
137-
WhatItDoes: "Checks if a value is a real array.",
138-
WhyItsUseful: "Helps avoid errors when working with array-like objects.",
139-
},
140-
{
141-
Method: "Array.entries()",
142-
WhatItDoes: "Returns key-value pairs of an array.",
143-
WhyItsUseful: "Useful for iterating with index & value.",
144-
},
145-
{
146-
Method: "Array.keys()",
147-
WhatItDoes: "Returns an iterator of indices (keys).",
148-
WhyItsUseful: "Helpful when you need to work with indices only.",
149-
},
150-
{
151-
Method: "Array.values()",
152-
WhatItDoes: "Returns an iterator of values.",
153-
WhyItsUseful: "Useful for looping through elements.",
154-
},
155-
{
156-
Method: "Array.fill()",
157-
WhatItDoes: "Fills an array with a specific value.",
158-
WhyItsUseful: "Useful for creating arrays with default values.",
159-
},
160-
{
161-
Method: "Array.copyWithin()",
162-
WhatItDoes: "Copies part of an array to another position.",
163-
WhyItsUseful: "Useful for reordering elements in-place.",
164-
},
165-
];
166124

167-
// Displaying each method information
168-
arrayMethods.forEach((method) => {
169-
console.log(`Method: ${method.Method}`);
170-
console.log(`What It Does: ${method.WhatItDoes}`);
171-
console.log(`Why It's Useful: ${method.WhyItsUseful}`);
172-
console.log("----------------------------------");
173-
});
125+
// NOTE: What is an array-like object ?
126+
/*
127+
An array-like object is a object that:-
128+
1. Has a length property
129+
2. Has values stored at numeric indices like 0, 1, 2, etc.
130+
3. But it's not a real array (doesn't have array methods like .map(), .forEach())
131+
*/
132+
133+
134+
// INFO: Advanced Array Utility Functions
174135

175136
/*
176-
Array.from()
137+
Array.from()
177138
Converts array-like objects or iterables to real arrays.
178139
*/
140+
179141
// Converting a string (array-like object) to an array
180142
const str = "Hello";
181143
const arrFromStr = Array.from(str);
@@ -184,10 +146,22 @@ console.log(arrFromStr); // ['H', 'e', 'l', 'l', 'o']
184146
// Creating an array from a NodeList (array-like object)
185147
const divs = document.querySelectorAll("div");
186148
const divArray = Array.from(divs);
187-
console.log(divArray); // Now you can use array methods like .map(), .filter() on it.
149+
console.log(divArray);
150+
151+
// Convert a Set to an Array and Double the Numbers
152+
const settings = {
153+
multiplier: 2,
154+
};
155+
156+
const numbers = new Set([1, 2, 3, 4]);
157+
158+
const doubledArray = Array.from(numbers, function (value) {
159+
return value * this.multiplier;
160+
}, settings);
161+
console.log(doubledArray); // [2, 4, 6, 8]
188162

189163
/*
190-
Array.of()
164+
Array.of()
191165
Creates arrays from arguments.
192166
*/
193167
const arr1 = Array.of(5); // [5] - A single element array.
@@ -199,7 +173,7 @@ console.log(Array(5)); // Creates an array with 5 empty slots (not useful).
199173
console.log(Array(1, 2, 3)); // Same as Array.of(1, 2, 3)
200174

201175
/*
202-
Array.isArray()
176+
Array.isArray()
203177
Checks if a value is a real array.
204178
*/
205179
console.log(Array.isArray([1, 2, 3])); // true
@@ -211,22 +185,23 @@ const divs1 = document.querySelectorAll("div");
211185
console.log(Array.isArray(divs1)); // false
212186

213187
/*
214-
✅ Array.entries()
215-
Returns an iterator of index-value pairs.
188+
Array.entries()
189+
is a method is javascript that returns a new array iterator object containing key/value pairs of each index in the array.
190+
Each pair is returned in the format [index, value].
216191
*/
217-
const arr3 = ["a", "b", "c"];
218-
const entries = arr3.entries();
192+
const colors = ["red", "green", "blue"];
193+
const iterator = colors.entries();
219194

220-
for (let [index, value] of entries) {
195+
for (const [index, value] of iterator) {
221196
console.log(index, value);
222197
}
223-
// Output:
224-
// 0 'a'
225-
// 1 'b'
226-
// 2 'c'
198+
199+
// we can also use .next() method manually
200+
console.log(iterator.next()); // { value: [ 0, "red" ], done: false }
201+
console.log(iterator.next().value; // [ 0, "red" ]
227202

228203
/*
229-
Array.keys()
204+
Array.keys()
230205
Returns an iterator of indices (keys).
231206
*/
232207
const arr4 = ["x", "y", "z"];
@@ -241,7 +216,7 @@ for (let key of keys) {
241216
// 2
242217

243218
/*
244-
Array.values()
219+
Array.values()
245220
Returns an iterator of values.
246221
*/
247222
const arr5 = [10, 20, 30];
@@ -256,28 +231,27 @@ for (let value of values) {
256231
// 30
257232

258233
/*
259-
Array.fill()
234+
Array.fill()
260235
Fills an array with a specific value.
261236
*/
262237
const arr6 = new Array(5).fill(0); // Creates an array of length 5 and fills it with 0.
263238
console.log(arr6); // [0, 0, 0, 0, 0]
264239

265240
// Partially filling an array
266241
const arr7 = [1, 2, 3, 4, 5];
267-
arr7.fill(9, 1, 3);
242+
arr7.fill(9, 1, 3); // value, startIndex, endIndex
268243
console.log(arr7); // [1, 9, 9, 4, 5]
269244

270245
/*
271-
Array.copyWithin()
272-
Copies part of an array to another position.
246+
Array.copyWithin()
247+
Copies part of the array to another location in the same array, without changing its length.
273248
*/
274249
const arr8 = [10, 20, 30, 40, 50];
275-
arr8.copyWithin(0, 3);
250+
arr8.copyWithin(0, 3); // target, startIndex
276251
console.log(arr8); // [40, 50, 30, 40, 50]
277252

278-
// Copying elements within the array (target, start, end)
279253
const arr9 = [1, 2, 3, 4, 5];
280-
arr9.copyWithin(1, 3, 5);
254+
arr9.copyWithin(1, 3, 5); // target, startIndex, endIndex
281255
console.log(arr9); // [1, 4, 5, 4, 5]
282256

283257
// NOTE: Quizzes
@@ -330,4 +304,4 @@ console.log(arr14); // [1, 2, 0, 0, 5]
330304
// INFO: Array.copyWithin()
331305
const arr15 = [1, 2, 3, 4, 5];
332306
arr15.copyWithin(2, 0, 2);
333-
console.log(arr15); // [3, 4, 5, 4, 5]
307+
console.log(arr15); // [1, 2, 1, 2, 5]

0 commit comments

Comments
 (0)