Now in the end what we did here in the last lecture with for each is we transformed the array right.
We in the end transformed it from an array of prices into an array of objects where of course also each
price changed.
We can do it with for each year but there is an easier way because what we have to do with for each
or with for loops is we have to manage our new separate array onto which we push these transformed items.
We can do that but there's a shorter alternative.
So if I copy that and then comment it out and repeat this example let's get rid of the text adjusted
prices array like this.
Let's get rid of this common here also to save some space.
And now instead of for each we can use map and numbers special method a way lable on erase.
Now map has the job of taking an array running a function which has this form on every item in the array
and then and that's important that function should now return a new element for every element in the
array a possibly transformed element.
So for each this function which you pass to for each doesn't have to return anything you can do whatever
you wanted for each for map.
This function which you pass to it has to do something has to return something it has to return the
mapped that transformed the new element for that array.
So here I would return prize object for example.
And that means that now it is will replace every element in the array on which we call map.
With this adjusted element however that's not totally true.
It doesn't really replace it.
The original array stays untouched.
Instead it returns a brand new array with a brand new address and memory where every element has been
changed.
So now here we can get our tax adjusted prices stored in a constant.
And prices themselves will be untouched as I can prove by logging them here.
Tax adjusted prices however will be d Well transformed.
Prices with the help of map.
So if we save that and reload here we see that as the original array nothing changed there.
And here we have t transformed array with our object in there so debt did indeed change.
So map is a really nice method if you want to transform an array because it makes changing elements
and getting a brand new array based on these changed elements.
Super super easy.