Animations
API
Arrays
Async
Basics
Challenges
Classes
Console
Dates
Debugging
DOM Elements
DOM Methods
DOM Properties
Event Listeners
Flow Control
Forms
Functions
Global Functions
JSON
Keywords
Libraries (3rd party)
Math
Modules
Objects
Snippets
String
Types
Widgets
Window Object
https://www.w3schools.com/jsref/jsref_obj_array.asp
The map()
method creates a new array with the results of calling a function for
every array element. The map()
method calls the provided function once for each element in an array, in order.
map()
does not execute the function for array elements without values.
The return type is a new array. For each element in the orig array, it will call a function and add the results to the new array .
https://www.w3schools.com/jsref/jsref_map.asp
function Capitalize(fruit)
{
return fruit.toUpperCase();
}
let fruitsArray = ["Banana", "Orange", "Apple", "Mango"];
let capFruitsArray = fruits.map(Capitalize)
document.getElementById("demo").innerHTML = capFruitsArray.join(" - ");
You can also use lambda expressions with .map()
.
let lcFruits = fruitsArray.map(f => f.toLowerCase());
document.getElementById("demo2").innerHTML = lcFruits.join(" - ");