# JavaScript Arrays: Properties, Methods, and Manipulation (Part 7 of 7)

## Метаданные

- **Канал:** Corey Schafer
- **YouTube:** https://www.youtube.com/watch?v=qxzp4X6sfGo
- **Дата:** 18.03.2015
- **Длительность:** 7:42
- **Просмотры:** 6,203

## Описание

JavaScript Arrays: Properties, Methods, and Manipulation (Part 7 of 7)

In this series, we will take an in-depth look at JavaScript Arrays and everything we can do with them.

In part 7, we will go over: map() and also chain together some of the methods we have gone over in this series.

This is the last video in the series. I hope it has been helpful.

Part 1: https://youtu.be/8JgU2WmrZXI
Part 2: https://youtu.be/nAWVYFEzoY8
Part 3: https://youtu.be/cdPS-lmlwco
Part 4: https://youtu.be/JskeRdu_X8Q
Part 5: https://youtu.be/w4KF_lapbRI
Part 6: https://youtu.be/1gupsllu5wQ
...


✅ Support My Channel Through Patreon:
https://www.patreon.com/coreyms

✅ Become a Channel Member:
https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join

✅ One-Time Contribution Through PayPal:
https://goo.gl/649HFY

✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey's Public Amazon Wishlist
http://a.co/inIyro1

✅ Equipment I Use and Books I Recommend:
https://www.amazon.com/shop/coreyschafer

▶️ You Can Find Me On:
My Website - http://coreyms.com/
My Second Channel - https://www.youtube.com/c/coreymschafer
Facebook - https://www.facebook.com/CoreyMSchafer
Twitter - https://twitter.com/CoreyMSchafer
Instagram - https://www.instagram.com/coreymschafer/

## Содержание

### [0:01](https://www.youtube.com/watch?v=qxzp4X6sfGo&t=1s) Map Method

so what the map method does is it runs every element in your array through a given function and then it returns whatever that return value is it adds it to a return array so for example let me just go ahead and show you what if we had an array of numbers here and let's pretend that this array of numbers that we were getting in was some money and we wanted to add tax to every one of these elements so if I wrote a function here and called this add tax then I can

### [0:40](https://www.youtube.com/watch?v=qxzp4X6sfGo&t=40s) Add Tax

pass in the element then I can return let's see I can return a dollar sign and then we can add this element times let's say that we want a six percent tax so let's do 1. 06 and then we'll just do a two fixed here on the end of two so that it keeps that to two decimal places and then let's uncomment out this mapped variable here and let's set this equal to array18. map and we want to map this array to this add tax function and save that and output this to the HTML and now you can see that the original right here all we had was an array of integers 10 20 30 40 50 and now this mapped version is every one of these elements run through this add tax function which puts the dollar sign on and Returns the tax value of all of the integers that we had so 1060 2120 3180 4240 and 53 dollars so yeah that's pretty cool just to show you another example here if we have an array of names then I could do a function called to Upper and then pass in the element and I could just return the element Dot to uppercase and comment out this varmapped and then set this equal to array 18 dot Matt and map this to the two upper function save that and let's output this to the HTML and save it and now you can see whenever I Echo out this mapped variable it is this entire array but all of these are now have two uppercase applied to them so you know writing something like this as uh is a lot more readable and a lot cleaner than doing a bunch of for loops and running through your array and doing things to every element that way plus you don't have to worry about these out of bounds exceptions and things like that you know if I go in here and start adding stuff to the end then you know it will just take care of that automatically so yeah the map function is very useful if you have a lot of data that you have coming in through an array and you want to do the same thing to every element in that array then the map function is definitely something that you want to check out um so that is the map method so I thought we could take a really quick look at how to change some of these methods together you know there's a lot of useful methods that we've gone over but uh you know sometimes uh they don't meet all your needs so you can actually um change some of these together and do some pretty neat stuff so in this example we have an array of 10 20 30 40 50 and we have that same ad tax method that we had before but instead of returning a string now we are returning I'm going to make sure that this is a number here we're returning a number that is the um the number times the six percent uh sales tax and then so let's go ahead and look at this I'm going to call this result 19. and let me copy that and output it through the screen so if I do what we did before taking a look at the matte method if I do array 19 that mat and then map that to the add tax function and then printed that out you can see that this is 10. 6 21 everything that we expect it's all the numbers but now say that each of these were individual items in a shopping cart or something like that and now not only did we want to add the tax but we wanted to Total all those together well I could do a function called sum total and I could just do the previous value the current value and this will be used with the reduce function and I can return previous Plus current and then down here I can just do dot reduce and just chain this onto the end of our all right the result that we already have then do reduce sum total save that and you can see we have 159 so all these added up together is 150 and plus that six percent sales tax that we put on there is 159 result another common example you might see if uh if you want to reverse a string in

### [6:06](https://www.youtube.com/watch?v=qxzp4X6sfGo&t=366s) Reverse a String

JavaScript you can do uh let me just do a stream here and I'll do let me uncomment out this output here so if I say please reverse me then this string we can split this now split's not a method we looked at because that is a string that's a method for a string not the array but we can split this and what this does is it turns this string into an array that we can work with now so now we have this array of characters down here called please reverse me and now we can just do what we did before with our arrays we can call Dot reverse on this and save that and now we have the Reversed version of that array and then we can just call the join method that we looked at and join that back together and now we have that this initial string here we split that then we reversed it then we joined it back together which gives us the original string reversed so you know things like that are pretty neat um so yeah I know this video was has been getting kind of long but hopefully it was useful for you guys and you know you learned some neat stuff about JavaScript arrays and how to manipulate them you know if you have any further questions feel free to ask in the comments be sure to subscribe to future videos and tutorials and thank you guys for watching

---
*Источник: https://ekstraktznaniy.ru/video/12773*