Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  1. Map:
  2.  
  3. map is a method that is available to any array.
  4.  
  5. It takes in a callback function which is called once for every element within the array
  6.  
  7. The return value of each function call will be added to a new array which will be the
  8. final return value once all elements have been iterated through
  9.  
  10. Note: Map returns a new array, it does not in itself modify the original array.
  11.  
  12. Here is a simple example of how map works - for now pay attention to the original array
  13. and the new array this is returned.
  14.  
  15. Then we will go over in greater detail the mechanics of how map is working behind the scenes
  16. to create the new array.
  17.  
  18. var nums = [1, 2, 3];
  19.  
  20. We will use this array to generate the numsDoubled array.
  21.  
  22. nums.map(function(num) {
  23. return num * 2;
  24. });
  25.  
  26. Because the return value of calling map on an array will be a new array let's
  27. store the return value in a variable:
  28.  
  29. let numsDoubled = nums.map(function(num) {
  30. return num * 2;
  31. });
  32.  
  33. nums still contains the original array i.e:
  34. [1, 2, 3];
  35.  
  36. numsDoubled is the newly created array with each number doubled i.e:
  37. [2, 4, 6];
  38.  
  39. It should hopefully be clear here that it is the callback function which is responsible for
  40. taking in the value of each element one at a time and returning some value.
  41.  
  42. The same result could also be achieved by using a traditional for loop
  43. however map can achieve the same result in a more concise implicit way.
  44.  
  45. When map is called a function is passed as the first argument. This function
  46. will be called once for each element within the array.
  47.  
  48. In this case there are three elments in the array so the function will be called
  49. three times.
  50.  
  51. This callback function can accept three different parameters, however for now
  52. we will focus on the first parameter.
  53.  
  54. This first parameter represents the value of the current element within the array. The actual looping
  55. mechanism is implicit meaning that the corresponding value will change for each iteration.
  56.  
  57. Remember that the function is called for once iteration so it always knows which element within the
  58. array that it is referring to.
  59.  
  60. The return value from this callback function is used to populate the new array this is created.
  61.  
  62. Map will add a new element to this array for each element in the initial array.
  63.  
  64. Generally you will use the value that is passing in to create the new value in the new array.
  65.  
  66. For example we want to use our nums array to create a new array where each number from the
  67. original array is doubled
  68.  
  69. If the return keyword is not used within the callback function the default value of undefined
  70. is added to the new array.
  71.  
  72. This callback function also takes two other arguments, index and the array itself - these are
  73. optional but helpful.
  74.  
  75. It is important to understand that arguments are passed implicitly by nature of how the
  76. map method works rather than being passed in by the author of the code - this is a fairly
  77. practise in many popular JavaScript libraries
  78.  
  79. The index is the current index of the array, for example in the first iteration the
  80. index is 0, the next it is 1 etc
  81.  
  82. The original is passed as the third argument if needed.
  83.  
  84. Note that all of these are passed to the callback function but you only need to indicate the parameters
  85. that you need.
  86.  
  87. In our example we only needed the value to get the result that we wanted:
  88.  
  89. let numsDoubled = nums.map(function(num) {
  90. return num * 2;
  91. });
  92.  
  93. In other situations you might also want to pass the index along.
  94.  
  95. The value, index and array parameters can be called anything because the value for
  96. each parameter is dependent on the position and not the name.
  97.  
  98. The first parameter is always the value
  99. The second parameter is always the index
  100. The third is always the original array.
  101.  
  102. The map method in addition to the callback function can also receive a second argument but
  103. that isn't relevant for what we are doing so I will leave it out for now.
  104.  
  105. So to recap, map is a method on the array that takes in a callback function and this function
  106. is run once for each element within the array and then returns a new array based on what was returned.
  107.  
  108. Many other array methods work similar to map so once you understand how map works you will also
  109. understand how other methods work like forEach, filter, etc etc
  110.  
  111. Although understanding how map works to begin with can be difficult once you understand it helps to keep code
  112. minimal and to the point.
  113.  
  114. The less code in the application that easier it will be to make sense of it and there is less
  115. opportunity that any errors may be created (because there is less code).
  116.  
  117.  
  118. A glossary of terms:
  119.  
  120. Implicit: Something that is implied rather than directly stated. For example the map method
  121. does most of its work implicitly. Generally implicit code will be less verbose than explicit code
  122.  
  123. Explicit: Showing the process/steps taken to achieve a certain outcome. A for loop is a
  124. good example of an explicit mechanism. Generally explicit code will be more verbose than implicit code.
  125.  
  126. Note: There are advantages and disadvantages to explicit and implicit mechanisms.
  127.  
  128. Implicit mechanisms are more concise and require less mental processing so long
  129. as you are familiar with them.
  130.  
  131. Learning how they work may be difficult because generally the way that the result is obtained
  132. is hidden - there is more work being done than you can see.
  133.  
  134. Implicit code is helpful when the result is obvious based on what input you gave it and what
  135. result you received.
  136.  
  137. Explicit code can be verbose which means that you will have to write and read more code
  138. and there is more chance of causing some kind of error.
  139.  
  140. The advantage is that you can look in fine detail the step by step process by which a result
  141. was achieved.
  142.  
  143. Callback function: A function that is passed as an argument to another function
  144. and which is called by that as part of the internal workings of the function of
  145. which it is passed
  146.  
  147. Method: In JavaScript the term method essentially refers to a function that is
  148. called in relation to an object. An array is considered an object in JavaScript
  149. and so any array has access to the map method.
  150.  
  151. Return value: Every function in JavaScript returns a value either an explicit value or the keyword
  152. undefined if there is no explicit use of the return keyword
  153.  
  154. Iterations: Doing something multiple times. Each time the data available is
  155. likely to be different
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement