article

native JavaScript Array method - Array.map()

[WHAT]

  1. [] by Don Sagrott, founder @ sospep.com - the Array.map() function can be used to create a copy of an existing (source) array with elements that meet a given condition that is specified in the callback function that is passed into the map
  2. [] generates a new array based on an existing array,
  3. [] takes a callback function as an arguement,
  4. [] walks through each item of the array and executes the given callback function on that element
  5. [] returns the new array

[WHY]

  1. [] this article is intended to be a quick 1 page reference to the array method Map
  2. [] along with a simple explanation of the method
  3. [] and a simple code example ( with context )
  4. [] using the Array.Map() method VS using ] a for loop - we dont have to write/manage the index code,

[WHERE]

  1. [] READ THE FULL ARTICLE
    1. [] see below

[WHEN]

  1. [] 2017-10-13 -
  2. [] 2019-11-23- updated, split into individual articles for each of the covered Array Methods

[EXAMPLE]

  1. [] The Array we will be working with is defined below and is a list of car model names, each array element is a "string" value and there are 5 initial elements, the array's name is cars. 
  2. [] var cars = ["corvette", "camaro", "mustang", "charger", "GTR"] ; 
  3. [] - EXAMPLE - lets create a new array of car objects from our cars array

Cars = cars.map( function(element, index){

  return {modelname:element};

} )

// [ { modelname: Corvette},{ modelname:Camaro}, { modelname:Mustang}, { modelname:Charger},{ modelname: GTR} ]

[HOW-TO]

  1. [] use map - to create a new array of car "objects", based on the car names in the original array,

[REFERENCE]

  1. [] # # - CREATE-article# 5764 (this)
  2. [] # # - MDN - Array.prototype.Map() -

[RELATED]

  1. [] # 5763 - Array.forEach()
  2. [] # 5764 - Array.map() 
  3. [] # 5772 - Array.reduce()
  4. [] # # - Array.filter()
  5. [] # # - Array.find()
Details Photos Edit more

Details

ID: 5764

NAME: JavaScript-Array-Methods-Map

DESCRIPTION: JavaScript-Array-Methods-Map - by Don Sagrott, founder @sospep.com - using the native JavaScript array method of Map to manipulate your JavaScript arrays

AUTHOR: article.author/s

EDITOR: article.editor/s

PUBLISHER: article.publisher/s

STATUS: Write

PRIORITY: -5

OWNER ID: 75

Content Photos Edit more

photos

page_photo

actions

Email Email-Owner SMS and