|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Transforming JavaScript Arrays using map()" |
| 4 | +date: 2020-04-19 22:00:00 -0700 |
| 5 | +tags: [JavaScript, Arrays, Objects] |
| 6 | +comments: true |
| 7 | +--- |
| 8 | + |
| 9 | +[Array.prototype.map()][Array.prototype.map()] method creates and returns a new array with the results of calling a provided function on every element in the calling array. |
| 10 | + |
| 11 | +We can use this method to transform or change elements of an array. |
| 12 | + |
| 13 | +In the following example, we add number `10` to every number in the initial array `nums`. The resultant array returned by `map()` is stored in the variable `newNums`. The original array isn't modified. |
| 14 | + |
| 15 | +## Example |
| 16 | + |
| 17 | +{% highlight javascript %} |
| 18 | + |
| 19 | +const nums = [2, 4, 6, 8]; |
| 20 | + |
| 21 | +const newNums = nums.map(num => num + 10); |
| 22 | + |
| 23 | +for (let num of newNums) { |
| 24 | + console.log(num); |
| 25 | +} |
| 26 | + |
| 27 | +{% endhighlight %} |
| 28 | + |
| 29 | +## Output |
| 30 | + |
| 31 | +{% highlight javascript %} |
| 32 | + |
| 33 | +12 |
| 34 | +14 |
| 35 | +16 |
| 36 | +18 |
| 37 | + |
| 38 | +{% endhighlight %} |
| 39 | + |
| 40 | +Let us look at another example of using `map()` on an array containing objects. |
| 41 | + |
| 42 | +## Example |
| 43 | + |
| 44 | +{% highlight javascript %} |
| 45 | + |
| 46 | +const productsInUSD = [ |
| 47 | + { "item": "pen", "price": "2" }, |
| 48 | + { "item": "pencil", "price": "1" }, |
| 49 | + { "item": "notebook", "price": "3" } |
| 50 | +]; |
| 51 | + |
| 52 | +const productsInINR = productsInUSD.map(product => { |
| 53 | + return { |
| 54 | + "item": product.item, |
| 55 | + "price": product.price * 75 |
| 56 | + }; |
| 57 | +}); |
| 58 | + |
| 59 | +for (let product of productsInINR) { |
| 60 | + console.log(`${product.item} - Rs.${product.price}`); |
| 61 | +} |
| 62 | + |
| 63 | +{% endhighlight %} |
| 64 | + |
| 65 | +## Output |
| 66 | + |
| 67 | +{% highlight javascript %} |
| 68 | + |
| 69 | +pen - Rs.150 |
| 70 | +pencil - Rs.75 |
| 71 | +notebook - Rs.225 |
| 72 | + |
| 73 | +{% endhighlight %} |
| 74 | + |
| 75 | +We can also use latest ES6 features and write the `map()` method as: |
| 76 | + |
| 77 | +{% highlight javascript %} |
| 78 | + |
| 79 | +const productsInINR = productsInUSD.map(product => ({ |
| 80 | + ...product, |
| 81 | + "price": product.price * 75 |
| 82 | +})); |
| 83 | + |
| 84 | +{% endhighlight %} |
| 85 | + |
| 86 | +## Avoid mutating original array |
| 87 | + |
| 88 | +When using `map()` on an array of objects, be careful to avoid mutating (modifying) original array and make sure to return a new object from the callback function. |
| 89 | + |
| 90 | +{% highlight javascript %} |
| 91 | + |
| 92 | +// Don't do this. |
| 93 | +const productsInINR = productsInUSD.map(product => { |
| 94 | + let newProduct = product; |
| 95 | + newProduct.price = product.price * 75; |
| 96 | + return newProduct; |
| 97 | +}); |
| 98 | + |
| 99 | +{% endhighlight %} |
| 100 | + |
| 101 | +At first glance, the above code looks good. But we should observe that we are assigning an object reference to `newProduct`, therefore modifying `newProduct` also modifies the original object. |
| 102 | + |
| 103 | +If we print `productsInUSD` array elements to the console, we can see that they are modified and now contain prices in INR. This isn't desired and breaks functionality in other parts of our application. |
| 104 | + |
| 105 | +To avoid this, clone the object before assigning. There are several ways to clone an object, let us use `JSON.parse(JSON.stringify(object))` for this example. |
| 106 | + |
| 107 | +{% highlight javascript %} |
| 108 | + |
| 109 | +const productsInINR = productsInUSD.map(product => { |
| 110 | + let newProduct = JSON.parse(JSON.stringify(product)); |
| 111 | + newProduct.price = product.price * 75; |
| 112 | + return newProduct; |
| 113 | +}); |
| 114 | + |
| 115 | +{% endhighlight %} |
| 116 | + |
| 117 | +This doesn't modify the original array and prevents any unintended effects. |
| 118 | + |
| 119 | + |
| 120 | +[Array.prototype.map()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map |
| 121 | +{:target="_blank"} |
0 commit comments