SlideShare a Scribd company logo
1 of 15
Download to read offline
Array Map Method
What is the map method?
What it does is go through
every element of the array,
and returns a new array.
What is the array map method?
Each element X at index i is
transformed/mapped to a
new element Y at index i of
the new array.
Illustration
1
2
3
1
4
6Map: 2*input
Array Map method be looking like?
let new_array = arr.map(function callback( currentValue[,
index[, array]]) {
// return element for new_array
}[, thisArg])
Other ways than map
● forEach
● for..of
● for
Other ways than map
forEach
const array = [1, 2, 3]
const new_array = []
array.forEach(x => new_array.push(x * x))
console.log(new_array) // [1, 4, 9]
Other ways than map
for..of
const array = [1, 2, 3]
const new_array = []
for ( let x of array)
new_array.push(x * x )
console.log(new_array) // [1, 4, 9]
Other ways than map
for
const array = [1, 2, 3]
const new_array = []
for ( let x = 0; x < array.length; x++)
new_array.push(array[x] * array[x])
console.log(new_array) // [1, 4, 9]
And finally Map
const array = [1, 2, 3]
const new_array = array.map( x => x * x)
console.log(new_array) // [1, 4, 9]
Calculate area of rectangles 1
const rectangles = [ { length: 2, width: 3 }, { length: 5, width: 2 }, { length:
10, width: 3 }]
const areaRectangles = rectangles.map( x => x.length * x.width)
console.log(areaRectangles) // [1, 4, 9]
Calculate area of rectangles 2
const rectangles = [ { length: 2, width: 3 }, { length: 5, width: 2 }, { length:
10, width: 3 }]
const areaRectangles = rectangles.map( x => x.area = x.length * x.width)
console.log(areaRectangles) // [ { length: 2, width: 3 , area: 6}, { length:
5, width: 2, area: 10 }, { length: 10, width: 3, area: 30 }]
Sample angular code
this.commandService .getCommands( {[queryName]: queryValue })
.subscribe((filtered: any) => {
this.commands = filtered.commands.map( (el: any) => {
return {
agent: el.user.displayName,
title: el.name,
client: el.sendername,
date: el.createdAt,
};
});
this.commandTableSource.load(this.commands);
});
Sample code React
<div className="gallery">
{ this.props.images.map((el, i) => <img alt={el.item} key={i} src={el.url} />)}
</div>
Questions
THE END

More Related Content

What's hot

January 7, 2014 function notation
January 7, 2014  function notationJanuary 7, 2014  function notation
January 7, 2014 function notation
khyps13
 
Function Operations
Function OperationsFunction Operations
Function Operations
swartzje
 
4 2 operations on functions
4 2 operations on functions4 2 operations on functions
4 2 operations on functions
hisema01
 
Operations With Functions May 25 2009
Operations With Functions May 25 2009Operations With Functions May 25 2009
Operations With Functions May 25 2009
ingroy
 
Day 2 examples u8f13
Day 2 examples u8f13Day 2 examples u8f13
Day 2 examples u8f13
jchartiersjsd
 
7.3 power functions and function operations
7.3 power functions and function operations7.3 power functions and function operations
7.3 power functions and function operations
hisema01
 

What's hot (18)

Py lecture5 python plots
Py lecture5 python plotsPy lecture5 python plots
Py lecture5 python plots
 
AP Calculus 1984 FRQs
AP Calculus 1984 FRQsAP Calculus 1984 FRQs
AP Calculus 1984 FRQs
 
January 7, 2014 function notation
January 7, 2014  function notationJanuary 7, 2014  function notation
January 7, 2014 function notation
 
Function Operations
Function OperationsFunction Operations
Function Operations
 
Binary space partition
Binary space partitionBinary space partition
Binary space partition
 
Day 2a examples
Day 2a examplesDay 2a examples
Day 2a examples
 
Geo Spatial Plot using R
Geo Spatial Plot using R Geo Spatial Plot using R
Geo Spatial Plot using R
 
Data visualization with multiple groups using ggplot2
Data visualization with multiple groups using ggplot2Data visualization with multiple groups using ggplot2
Data visualization with multiple groups using ggplot2
 
Matlab graphics
Matlab graphicsMatlab graphics
Matlab graphics
 
4 2 operations on functions
4 2 operations on functions4 2 operations on functions
4 2 operations on functions
 
Md2k 0219 shang
Md2k 0219 shangMd2k 0219 shang
Md2k 0219 shang
 
Operations With Functions May 25 2009
Operations With Functions May 25 2009Operations With Functions May 25 2009
Operations With Functions May 25 2009
 
Ee693 sept2014quizgt2
Ee693 sept2014quizgt2Ee693 sept2014quizgt2
Ee693 sept2014quizgt2
 
Ee693 sept2014quizgt1
Ee693 sept2014quizgt1Ee693 sept2014quizgt1
Ee693 sept2014quizgt1
 
Day 2 examples u8f13
Day 2 examples u8f13Day 2 examples u8f13
Day 2 examples u8f13
 
Plotting position and velocity
Plotting position and velocityPlotting position and velocity
Plotting position and velocity
 
Matlab plotting
Matlab plottingMatlab plotting
Matlab plotting
 
7.3 power functions and function operations
7.3 power functions and function operations7.3 power functions and function operations
7.3 power functions and function operations
 

Similar to Javascript Array map method

Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
Aseelhalees
 
1) IntroductionThis practical work consists of developing
1) IntroductionThis practical work consists of developing1) IntroductionThis practical work consists of developing
1) IntroductionThis practical work consists of developing
AbbyWhyte974
 
1) IntroductionThis practical work consists of developing
1) IntroductionThis practical work consists of developing1) IntroductionThis practical work consists of developing
1) IntroductionThis practical work consists of developing
MartineMccracken314
 
SaveI need help with this maze gui that I wrote in java, I am tryi.pdf
SaveI need help with this maze gui that I wrote in java, I am tryi.pdfSaveI need help with this maze gui that I wrote in java, I am tryi.pdf
SaveI need help with this maze gui that I wrote in java, I am tryi.pdf
arihantstoneart
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdf
arri2009av
 

Similar to Javascript Array map method (20)

Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
 
NumPy_Broadcasting Data Science - Python.pptx
NumPy_Broadcasting Data Science - Python.pptxNumPy_Broadcasting Data Science - Python.pptx
NumPy_Broadcasting Data Science - Python.pptx
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdf
 
Scala Collections
Scala CollectionsScala Collections
Scala Collections
 
Scala collections
Scala collectionsScala collections
Scala collections
 
bobok
bobokbobok
bobok
 
1) IntroductionThis practical work consists of developing
1) IntroductionThis practical work consists of developing1) IntroductionThis practical work consists of developing
1) IntroductionThis practical work consists of developing
 
1) IntroductionThis practical work consists of developing
1) IntroductionThis practical work consists of developing1) IntroductionThis practical work consists of developing
1) IntroductionThis practical work consists of developing
 
Tutorial 2
Tutorial     2Tutorial     2
Tutorial 2
 
Functions in advanced programming
Functions in advanced programmingFunctions in advanced programming
Functions in advanced programming
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Array
ArrayArray
Array
 
Groovy collection api
Groovy collection apiGroovy collection api
Groovy collection api
 
Javascript
JavascriptJavascript
Javascript
 
SaveI need help with this maze gui that I wrote in java, I am tryi.pdf
SaveI need help with this maze gui that I wrote in java, I am tryi.pdfSaveI need help with this maze gui that I wrote in java, I am tryi.pdf
SaveI need help with this maze gui that I wrote in java, I am tryi.pdf
 
Matlab Overviiew 2
Matlab Overviiew 2Matlab Overviiew 2
Matlab Overviiew 2
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdf
 
Introduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdf
Introduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdfIntroduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdf
Introduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdf
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Secretary_Game_With_Rejection.pdf
Secretary_Game_With_Rejection.pdfSecretary_Game_With_Rejection.pdf
Secretary_Game_With_Rejection.pdf
 

Recently uploaded

AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Recently uploaded (20)

Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 

Javascript Array map method

  • 2. What is the map method? What it does is go through every element of the array, and returns a new array.
  • 3. What is the array map method? Each element X at index i is transformed/mapped to a new element Y at index i of the new array.
  • 5. Array Map method be looking like? let new_array = arr.map(function callback( currentValue[, index[, array]]) { // return element for new_array }[, thisArg])
  • 6. Other ways than map ● forEach ● for..of ● for
  • 7. Other ways than map forEach const array = [1, 2, 3] const new_array = [] array.forEach(x => new_array.push(x * x)) console.log(new_array) // [1, 4, 9]
  • 8. Other ways than map for..of const array = [1, 2, 3] const new_array = [] for ( let x of array) new_array.push(x * x ) console.log(new_array) // [1, 4, 9]
  • 9. Other ways than map for const array = [1, 2, 3] const new_array = [] for ( let x = 0; x < array.length; x++) new_array.push(array[x] * array[x]) console.log(new_array) // [1, 4, 9]
  • 10. And finally Map const array = [1, 2, 3] const new_array = array.map( x => x * x) console.log(new_array) // [1, 4, 9]
  • 11. Calculate area of rectangles 1 const rectangles = [ { length: 2, width: 3 }, { length: 5, width: 2 }, { length: 10, width: 3 }] const areaRectangles = rectangles.map( x => x.length * x.width) console.log(areaRectangles) // [1, 4, 9]
  • 12. Calculate area of rectangles 2 const rectangles = [ { length: 2, width: 3 }, { length: 5, width: 2 }, { length: 10, width: 3 }] const areaRectangles = rectangles.map( x => x.area = x.length * x.width) console.log(areaRectangles) // [ { length: 2, width: 3 , area: 6}, { length: 5, width: 2, area: 10 }, { length: 10, width: 3, area: 30 }]
  • 13. Sample angular code this.commandService .getCommands( {[queryName]: queryValue }) .subscribe((filtered: any) => { this.commands = filtered.commands.map( (el: any) => { return { agent: el.user.displayName, title: el.name, client: el.sendername, date: el.createdAt, }; }); this.commandTableSource.load(this.commands); });
  • 14. Sample code React <div className="gallery"> { this.props.images.map((el, i) => <img alt={el.item} key={i} src={el.url} />)} </div>