Javascript Map Method

The map() method can be used to modify all elements of an array.   Below, I have a function that will take an array of sales prices, and mark them up by 10% to account for sales tax.  On line 3, if I return the item without the parseFloat function, it converts all the elements in the array to a string, so I use  the parseFloat function (to return a number with decimals) and the toFixed() method round to two decimal places.

Next, I will modify an array of objects using the map() method.  The addPrefix function will look at the sex of the item.  If the sex is male, it will add “Mr. ” to the name, and if the sex is female, it will add “Ms. ” to the name.

Leave a Reply

Your email address will not be published.