Lädt...


🔧 A Deep Dive into the `array.map` Method - Mastering JavaScript


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

The array.map function is a method available in JavaScript (and in some other languages under different names or syntax) that is used to create a new array populated with the results of calling a provided function on every element in the calling array. It's a powerful tool for transforming arrays.

👉 Download eBook - JavaScript: from ES2015 to ES2023

.

Here's a detailed look at how the array.map function works in JavaScript:

Syntax

array.map(callback(currentValue, index, array), thisArg)
  • callback: A function that is called for every element of the array. Each time the callback executes, the returned value is added to the new array.
    • currentValue: The current element being processed in the array.
    • index: The index of the current element being processed in the array.
    • array: The array map was called upon.
  • thisArg (optional): Value to use as this when executing the callback.

Example Usage

  1. Basic Example
const numbers = [1, 4, 9, 16];
const doubled = numbers.map(num => num * 2);

console.log(doubled); // [2, 8, 18, 32]
  1. Using Index
const numbers = [1, 4, 9, 16];
const withIndex = numbers.map((num, index) => `${index}: ${num}`);

console.log(withIndex); // ["0: 1", "1: 4", "2: 9", "3: 16"]
  1. Converting Data Types
const stringNumbers = ["1", "2", "3"];
const parsedNumbers = stringNumbers.map(str => parseInt(str));

console.log(parsedNumbers); // [1, 2, 3]

Key Points

  • Immutability: map does not change the original array. It creates a new array with the transformed elements.
  • Function Requirement: map requires a callback function. If you just want to copy an array, you should use slice or the spread operator.
  • Consistency: The new array will always have the same length as the original array.

Practical Use Cases

  1. Transforming Data

Converting an array of objects into an array of specific property values:

   const users = [
       { id: 1, name: "John" },
       { id: 2, name: "Jane" },
       { id: 3, name: "Doe" }
   ];
   const userNames = users.map(user => user.name);

   console.log(userNames); // ["John", "Jane", "Doe"]
  1. Extracting Data

Extracting URLs from an array of objects:

   const links = [
       { label: "Google", url: "http://google.com" },
       { label: "Facebook", url: "http://facebook.com" },
       { label: "Twitter", url: "http://twitter.com" }
   ];
   const urls = links.map(link => link.url);

   console.log(urls); // ["http://google.com", "http://facebook.com", "http://twitter.com"]
  1. Combining with Other Methods

Combining map with filter to first filter an array and then transform it:

   const numbers = [1, 2, 3, 4, 5, 6];
   const evenSquares = numbers.filter(num => num % 2 === 0).map(num => num * num);

   console.log(evenSquares); // [4, 16, 36]

Conclusion

The array.map function is a fundamental method in JavaScript for transforming arrays. It allows for the application of a function to each element of an array, resulting in a new array with the transformed elements. Understanding and using map effectively can lead to cleaner, more readable code, especially when dealing with data transformation tasks.

👉 Download eBook
javascript-from-es2015-to-es2023

...

🔧 Tìm Hiểu Về RAG: Công Nghệ Đột Phá Đang "Làm Mưa Làm Gió" Trong Thế Giới Chatbot


📈 39.47 Punkte
🔧 Programmierung

🔧 Unlock the Power of JavaScript Map: Don't Confuse it with Array map!


📈 38.77 Punkte
🔧 Programmierung

🔧 Array-like Objects in JavaScript: A Deep Dive


📈 37.18 Punkte
🔧 Programmierung

🔧 Deep dive into Array Data Structure


📈 36.63 Punkte
🔧 Programmierung

🔧 Array Manipulation: A Deep Dive into Insertions and Deletions


📈 36.63 Punkte
🔧 Programmierung

🔧 Array / Array Method / Function


📈 33.96 Punkte
🔧 Programmierung

🎥 Deep dive into Flutter deep linking


📈 33.5 Punkte
🎥 Video | Youtube

🔧 A Deep Dive Into Recommendation Algorithms With Netflix Case Study and NVIDIA Deep Learning Technology


📈 33.5 Punkte
🔧 Programmierung

🔧 Deep Dive into apple-app-site-association file: Enhancing Deep Linking on iOS


📈 33.5 Punkte
🔧 Programmierung

🔧 Deep Dive into apple-app-site-association file: Enhancing Deep Linking on iOS


📈 33.5 Punkte
🔧 Programmierung

🐧 ES6 Map an Array of Objects to Return an Array of Objects With New Keys


📈 33.3 Punkte
🐧 Linux Tipps

🔧 Array.from VS Array.prototype.map


📈 33.3 Punkte
🔧 Programmierung

🔧 Decoding the Conversation: A Deep Dive into Request and Response Objects in JavaScript


📈 31.73 Punkte
🔧 Programmierung

🔧 Deep Dive into JavaScript Promises: Patterns and Best Practices


📈 31.73 Punkte
🔧 Programmierung

🔧 A Deep Dive into Self-Referencing Objects and Circular References in JavaScript


📈 31.73 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Inheritance: A Deep Dive into Prototypal and Constructor Patterns


📈 31.73 Punkte
🔧 Programmierung

🔧 Unlocking JavaScript: A Deep Dive into Fundamentals.🚀🚀


📈 31.73 Punkte
🔧 Programmierung

🔧 A deep dive into converting between strings and numbers in JavaScript


📈 31.73 Punkte
🔧 Programmierung

🔧 How JavaScript's console.log() Surprised Me: A Deep Dive into Its Hidden Gems and Unexpected Behaviors


📈 31.73 Punkte
🔧 Programmierung

🔧 Deep Dive into Data structures using Javascript - Graph


📈 31.73 Punkte
🔧 Programmierung

🔧 Deep Dive into Functional Programming in Javascript


📈 31.73 Punkte
🔧 Programmierung

🔧 Day 2: Deep Dive into JavaScript Data Types, Strict Mode, and Basic Operations


📈 31.73 Punkte
🔧 Programmierung

🔧 Deep Dive into Functional Programming in Javascript


📈 31.73 Punkte
🔧 Programmierung

🔧 A Deep Dive into JavaScript Promise


📈 31.73 Punkte
🔧 Programmierung

🔧 Deep Dive into Data structures using Javascript - Priority Queue


📈 31.73 Punkte
🔧 Programmierung

🔧 Elevate Your JavaScript: A Deep Dive into Object-Oriented Programming✨


📈 31.73 Punkte
🔧 Programmierung

🔧 Async Made Easy: A Deep Dive into JavaScript Callbacks, Promises, and Async/Await


📈 31.73 Punkte
🔧 Programmierung

🔧 Async Made Easy: A Deep Dive into JavaScript Callbacks, Promises, and Async/Await


📈 31.73 Punkte
🔧 Programmierung

🔧 Day 13: Deep Dive into Object Properties, Getters & Setters, and Lexical Scope in JavaScript 🎉


📈 31.73 Punkte
🔧 Programmierung

🔧 Unraveling JavaScript: A Deep Dive into Hoisting, Temporal Dead Zone, and Variable States


📈 31.73 Punkte
🔧 Programmierung

🔧 Deep Dive into JavaScript Closures: How and When to Use Them


📈 31.73 Punkte
🔧 Programmierung

🔧 Deep Dive Into AI’s Inheritance Into Software Development


📈 31.18 Punkte
🔧 Programmierung

🔧 JavaScript Array Length – How to Find the Length of an Array in JS


📈 29.52 Punkte
🔧 Programmierung

matomo