Tech Junkie Blog - Real World Tutorials, Happy Coding!: JavaScript Array Methods: map() Method

Monday, November 15, 2021

JavaScript Array Methods: map() Method

The map() method is similar forEach() method which means that it performs a function, however it returns a new array with the returned values.  In this example we are going to perform the $1 discount of the oil prices using the map() method instead of the forEach() the difference here is that the map() method returns a new array instead of the original array, but the result will be the same.


    <script>
        var oilPrices = [70.15, 69.50, 71.23, 74.32, 76.99];

        var discounted = oilPrices.map(function (value) { return value -1; });

        console.log(discounted);
    </script>




The code above calls the map() method on the oil prices and returns a discounted value of $1 for each array element















1 comment:

Search This Blog