Learn to code and become a Web Developer with HTML, CSS, JavaScript , Reactjs, Wordpress, PHP, Mern & Nodejs knowledge
Managed by: @love_data
Post #3178
4.09K
β
JavaScript Arrays, Objects Array Methods π¦π§
Understanding how arrays and objects work helps you organize, transform, and analyze data in JavaScript. Here's a quick and clear breakdown:
1οΈβ£ Arrays in JavaScript
An array stores a list of values.
let fruits = ["apple", "banana", "mango"];
console.log(fruits[1]); // banana
βΆοΈ This creates an array of fruits and prints the second fruit (banana), since arrays start at index 0.
2οΈβ£ Objects in JavaScript
Objects hold keyβvalue pairs.
let user = {
name: "Riya",
age: 25,
isAdmin: false
};
console.log(user.name); // Riya
console.log(user["age"]); // 25
βΆοΈ This object represents a user. You can access values using dot (.) or bracket ([]) notation.
3οΈβ£ Array Methods β map, filter, reduce
πΉ map() β Creates a new array by applying a function to each item
let nums = [1, 2, 3];
let doubled = nums.map(n => n * 2);
console.log(doubled); // [2, 4, 6]
βΆοΈ This multiplies each number by 2 and returns a new array.
πΉ filter() β Returns a new array with items that match a condition
let ages = [18, 22, 15, 30];
let adults = ages.filter(age => age >= 18);
console.log(adults); // [18, 22, 30]
βΆοΈ This filters out all ages below 18.
πΉ reduce() β Reduces the array to a single value
let prices = [100, 200, 300];
let total = prices.reduce((acc, price) => acc + price, 0);
console.log(total); // 600
βΆοΈ This adds all prices together to get the total sum.
π‘ Extra Notes:
β’ map() β transforms items
β’ filter() β keeps matching items
β’ reduce() β combines all items into one result
π― Practice Challenge:
β’ Create an array of numbers
β’ Use map() to square each number
β’ Use filter() to keep only even squares
β’ Use reduce() to add them all up
π¬ Tap β€οΈ for more!
Understanding how arrays and objects work helps you organize, transform, and analyze data in JavaScript. Here's a quick and clear breakdown:
1οΈβ£ Arrays in JavaScript
An array stores a list of values.
let fruits = ["apple", "banana", "mango"];
console.log(fruits[1]); // banana
βΆοΈ This creates an array of fruits and prints the second fruit (banana), since arrays start at index 0.
2οΈβ£ Objects in JavaScript
Objects hold keyβvalue pairs.
let user = {
name: "Riya",
age: 25,
isAdmin: false
};
console.log(user.name); // Riya
console.log(user["age"]); // 25
βΆοΈ This object represents a user. You can access values using dot (.) or bracket ([]) notation.
3οΈβ£ Array Methods β map, filter, reduce
πΉ map() β Creates a new array by applying a function to each item
let nums = [1, 2, 3];
let doubled = nums.map(n => n * 2);
console.log(doubled); // [2, 4, 6]
βΆοΈ This multiplies each number by 2 and returns a new array.
πΉ filter() β Returns a new array with items that match a condition
let ages = [18, 22, 15, 30];
let adults = ages.filter(age => age >= 18);
console.log(adults); // [18, 22, 30]
βΆοΈ This filters out all ages below 18.
πΉ reduce() β Reduces the array to a single value
let prices = [100, 200, 300];
let total = prices.reduce((acc, price) => acc + price, 0);
console.log(total); // 600
βΆοΈ This adds all prices together to get the total sum.
π‘ Extra Notes:
β’ map() β transforms items
β’ filter() β keeps matching items
β’ reduce() β combines all items into one result
π― Practice Challenge:
β’ Create an array of numbers
β’ Use map() to square each number
β’ Use filter() to keep only even squares
β’ Use reduce() to add them all up
π¬ Tap β€οΈ for more!
- β€ 12