java-script:map-method
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
java-script:map-method [2023/08/11 16:14] – removed - external edit (Unknown date) 127.0.0.1 | java-script:map-method [2023/08/11 16:20] (current) – odefta | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Java Script Map Method====== | ||
+ | Map is a very common and useful method for creating a new array from an existing one by applying a function to every element. \\ | ||
+ | Syntax: | ||
+ | <code javascript> | ||
+ | const newArray = oldArray.map(function(currentValue, | ||
+ | // Return element for newArray | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | **currentValue**: | ||
+ | **index** (Optional): The index of the current value being processed. \\ | ||
+ | **array** (Optional): The original array. | ||
+ | |||
+ | < | ||
+ | The map method doesn' | ||
+ | </ | ||
+ | |||
+ | ===== Example 1 ===== | ||
+ | |||
+ | <code javascript> | ||
+ | const numbers = [1, 2, 3, 4, 5]; | ||
+ | const squares = numbers.map(number => number * number); | ||
+ | |||
+ | console.log(squares); | ||
+ | </ | ||
+ | |||
+ | ===== Example 2 ===== | ||
+ | |||
+ | Transform an array of strings into an array of objects: | ||
+ | <code javascript> | ||
+ | const strings = [' | ||
+ | |||
+ | const objects = strings.map((str, | ||
+ | return { name: str, id: index }; | ||
+ | }); | ||
+ | |||
+ | console.log(objects); | ||
+ | </ | ||
+ | |||
+ | We can also use an [[java-script: | ||
+ | <code javascript> | ||
+ | const strings = [' | ||
+ | |||
+ | const objects = strings.map((str, | ||
+ | |||
+ | console.log(objects); | ||
+ | </ | ||
+ | |||
+ | Output: | ||
+ | <code javascript> | ||
+ | [ | ||
+ | { name: ' | ||
+ | { name: ' | ||
+ | { name: ' | ||
+ | ] | ||
+ | </ | ||
+ | |||
+ | ===== Example 3 ===== | ||
+ | |||
+ | Imagine we have a collection of products, and we want to apply a discount to the products in even-indexed positions (0, 2, 4, etc.), and we want to calculate the average price before applying the discount. We'll make use of the value, index, and array parameters to accomplish this: | ||
+ | |||
+ | <code javascript> | ||
+ | const products = [ | ||
+ | { name: ' | ||
+ | { name: ' | ||
+ | { name: ' | ||
+ | { name: ' | ||
+ | { name: ' | ||
+ | ]; | ||
+ | |||
+ | const averagePrice = products.reduce((sum, | ||
+ | |||
+ | const discountedProducts = products.map((product, | ||
+ | if (index % 2 === 0 && array.length > 2) { | ||
+ | return { | ||
+ | name: product.name, | ||
+ | price: product.price - averagePrice * 0.1, // 10% of average price discount | ||
+ | }; | ||
+ | } | ||
+ | return product; | ||
+ | }); | ||
+ | |||
+ | console.log(discountedProducts); | ||
+ | </ | ||
+ | |||
+ | Output is: | ||
+ | <code javascript> | ||
+ | [ | ||
+ | { name: ' | ||
+ | { name: ' | ||
+ | { name: ' | ||
+ | { name: ' | ||
+ | { name: ' | ||
+ | ] | ||
+ | </ | ||
+ | |||
+ | Here, products at even indexes get a discount of 10% of the average price of all products. The **array** parameter is used to check that there are more than two products to ensure the average price calculation makes sense in context, and the **index** parameter is used to select the items to discount. | ||
+ | |||
+ | Here's how it works: | ||
+ | |||
+ | * The average price of the products is (1000 + 500 + 300 + 200 + 50) / 5 = 410. | ||
+ | * The 10% discount of the average price is 410 * 0.1 = 40. | ||
+ | * This discount is applied to every second product in the array (i.e., the products at index 0, 2, etc.), so the final prices of the ' | ||
+ | |||
+ | ===== Example 4 ===== | ||
+ | |||
+ | Transform an array of objects into an array of strings: | ||
+ | |||
+ | <code javascript> | ||
+ | const courses = [ | ||
+ | { name: ' | ||
+ | { name: ' | ||
+ | { name: ' | ||
+ | { name: ' | ||
+ | ]; | ||
+ | |||
+ | const coursesAsArray = courses.map(entry => entry.name); | ||
+ | console.table(coursesAsArray); | ||
+ | </ |