🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 3 Min Lesezeit
0

Using Map, Filter, and Reduce Effectively in JavaScript🔥

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

JavaScript’s map(), filter(), and reduce() are essential tools for working with arrays, especially when dealing with arrays of objects. They let you write clean, functional code for transforming, filtering, and aggregating data. Here’s how you can use them effectively:









1. map() – Transforming Data



The map() method is perfect for creating new arrays by transforming each element.



📌 Use Case: Extracting or reformatting object properties.



Examples:




CODE
const products = [
{ name: 'Laptop', price: 1000 },
{ name: 'Phone', price: 500 },
{ name: 'Tablet', price: 700 },
];

// Example 1: Extract product names
const productNames = products.map(product => product.name);
console.log(productNames); // ['Laptop', 'Phone', 'Tablet']

// Example 2: Apply a discount to all prices
const discountedProducts = products.map(product => ({
...product,
price: product.price * 0.9,
}));
console.log(discountedProducts);
// [
// { name: 'Laptop', price: 900 },
// { name: 'Phone', price: 450 },
// { name: 'Tablet', price: 630 }
// ]












2. filter() – Picking the Right Data



Use filter() to create a subset of data based on a condition.



📌 Use Case: Filtering objects based on their properties.



Examples:




CODE
const employees = [
{ name: 'Alice', role: 'Developer' },
{ name: 'Bob', role: 'Designer' },
{ name: 'Charlie', role: 'Developer' },
];

// Example 1: Filter developers
const developers = employees.filter(employee => employee.role === 'Developer');
console.log(developers);
// [
// { name: 'Alice', role: 'Developer' },
// { name: 'Charlie', role: 'Developer' }
// ]

// Example 2: Filter employees whose names start with 'A'
const namesStartingWithA = employees.filter(employee => employee.name.startsWith('A'));
console.log(namesStartingWithA);
// [{ name: 'Alice', role: 'Developer' }]












3. reduce() – Combining Data



The reduce() method aggregates data from an array into a single value or object.



📌 Use Case: Summing values or building new structures.



Examples:




CODE
const orders = [
{ id: 1, amount: 100 },
{ id: 2, amount: 200 },
{ id: 3, amount: 300 },
];

// Example 1: Calculate the total amount
const totalAmount = orders.reduce((total, order) => total + order.amount, 0);
console.log(totalAmount); // 600

// Example 2: Group orders by amount threshold
const groupedOrders = orders.reduce((groups, order) => {
const category = order.amount > 150 ? 'High' : 'Low';
groups[category] = groups[category] || [];
groups[category].push(order);
return groups;
}, {});
console.log(groupedOrders);
// {
// Low: [{ id: 1, amount: 100 }],
// High: [{ id: 2, amount: 200 }, { id: 3, amount: 300 }]
// }












Chaining Example



You can combine these methods to process data step by step.




CODE
const users = [
{ name: 'Alice', age: 25, isActive: true },
{ name: 'Bob', age: 30, isActive: false },
{ name: 'Charlie', age: 35, isActive: true },
];

// Get active users, double their age, and calculate the total
const totalAge = users
.filter(user => user.isActive) // Active users
.map(user => ({ ...user, age: user.age * 2 })) // Double their age
.reduce((sum, user) => sum + user.age, 0); // Sum their ages

console.log(totalAge); // 120 (Alice: 50, Charlie: 70)












Why Use These Methods?





  • Efficiency: Perform operations without manual loops.


  • Readability: Your code is easier to understand.


  • Immutability: They don’t modify the original array.



Mastering these methods with arrays of objects will supercharge your JavaScript skills! 🚀 Let me know your favorite use case in the comments! 👇

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Using Map, Filter, and Reduce Effectively in JavaScript🔥

Thematisch verwandte Begriffe: Using, Filter, Reduce, Effectively · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...