- Introduction to Arrays
- Working with Objects
- Array Methods
- Destructuring Arrays
- Destructuring Objects
Arrays are used to store multiple values in a single variable. They can hold a mix of data types, such as numbers, strings, and objects.
const numbers = [1, 2, 3, 4];
const mixedArray = ["Hello", 42, true];
- Access Elements:
array[index]
- Length:
array.length
- Add/Remove Elements:
push()
,pop()
,shift()
,unshift()
const fruits = ["apple", "banana"];
fruits.push("orange"); // Add at the end
console.log(fruits) // ["apple","banana","orange"]
console.log(fruits.pop()); // Remove from the end -> orange
fruits.unshift("banana") // Add at the begin
console.log(fruits) // ["banana","apple","banana"]
fruits.shift() // Remove from the begin
console.log(fruits) // ["apple","banana"]
Objects are key-value pairs used to store structured data.
const person = {
name: "John",
age: 30,
isStudent: false
};
- Dot Notation:
object.property
- Bracket Notation:
object["property"]
console.log(person.name); // John
console.log(person["age"]); // 30
person.job = "Developer";
person.age = 31;
Creates a new array by applying a function to each element of the original array.
const numbers = [1, 2, 3];
const squared = numbers.map(num => num ** 2);
console.log(squared); // [1, 4, 9]
Creates a new array with elements that pass a given condition.
const numbers = [1, 2, 3, 4];
const evens = numbers.filter(num => num % 2 === 0);
console.log(evens); // [2, 4]
Reduces an array to a single value by applying a function to an accumulator and each element.
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((acc, num) => acc + num, 0);
console.log(sum); // 10
Finds the first element that satisfies a condition.
const numbers = [1, 2, 3, 4];
const found = numbers.find(num => num > 2);
console.log(found); // 3
const arr = [1, 2, 3];
const index1 = arr.findIndex((x) => x > 2);
const index2 = arr.findIndex((x) => x > 3);
console.log(index1);// 2
console.log(index2);//-1
Checks if all elements satisfy a condition.
const numbers = [2, 4, 6];
console.log(numbers.every(num => num % 2 === 0)); // true
Checks if at least one element satisfies a condition.
const numbers = [1, 2, 3];
console.log(numbers.some(num => num > 2)); // true
Sorts an array in place.
const numbers = [3, 1, 4];
numbers.sort((a, b) => a - b); // Ascending
console.log(numbers); // [1, 3, 4]
numbers.sort((a, b) => b - a); // Descending
console.log(numbers); // [4, 3, 1]
Instead of manually accessing array elements, you can destructure them into variables.
Copy code
const arr = [1, 22, 3];
// Traditional (bad) way
const a = arr[0];
const b = arr[1];
const c = arr[2];
console.log(a, b, c); // Output: 1 22 3
// Using Destructuring (good way)
const [a1, b1, c1] = arr;
console.log(a1, b1, c1); // Output: 1 22 3
// Skipping values in the array
const [a2, , c2] = arr;
console.log(a2, c2); // Output: 1 3
Destructuring makes swapping values easier:
Copy code
let num1 = 10;
let num2 = 20;
console.log(num1, num2); // Output: 10 20
// Traditional swap using a temporary variable
let temp = num1;
num1 = num2;
num2 = temp;
console.log(num1, num2); // Output: 20 10
// Using Destructuring to swap values
[num1, num2] = [num2, num1];
console.log(num1, num2); // Output: 20 10
You can also destructure values returned from functions.
Copy code
function fun(Name, age) {
return [Name + " Khaled", age + 1];
}
[Name, age] = fun("Amr", 20);
console.log(Name, age); // Output: "Amr Khaled", 21
You can destructure nested arrays by using additional square brackets.
Copy code
const nestedArray = [
[1, 2],
[4, 6],
];
// Destructuring nested arrays
[ne1, ne2] = nestedArray; // [ [1, 2], [4, 6] ]
[[ne11, ne12], [ne21, ne22]] = nestedArray;
console.log(ne11, ne12); // Output: 1 2
console.log(ne21, ne22); // Output: 4 6
const university = {
name: "Ain Shams",
location: "Cairo",
TAs: ["ahmed", "mohamed", "anas", "ali"],
Drs: {
ahmedSalah: {
Age: 40,
teach: ["OS", "Algo"],
},
hanan: {
Age: 35,
teach: ["IOT", "Robot"],
},
},
};
// Destructuring top-level properties
const { name, location, TAs } = university;
console.log(name, location, TAs); // Output: "Ain Shams", "Cairo", ["ahmed", "mohamed", "anas", "ali"]
Renaming Variables with Destructuring: You can rename variables during destructuring:
const { name: uniName, location, TAs } = university;
console.log(uniName, location, TAs); // Output: "Ain Shams", "Cairo", ["ahmed", "mohamed", "anas", "ali"]
If a property doesn’t exist in the object, you can provide a default value:
const { students = [], TAs } = university;
console.log(students, TAs); // Output: [], ["ahmed", "mohamed", "anas", "ali"]
To destructure nested objects, reference the nested structure:
const {
ahmedSalah: {
Age,
teach: [Sub1, Sub2],
},
} = university.Drs;
console.log(Age, Sub1, Sub2); // Output: 40, "OS", "Algo"
Accessing properties at the wrong level (e.g., trying to destructure ahmedSalah directly from university instead of university.Drs):
const { ahmedSalah } = university; // This would be undefined
console.log(ahmedSalah); // Output: undefined
- Check all references.
- TaskLink