Besides objects, another crucial data structure in nodejs or in Javascript in general are arrays
like hobbies. An array is defined with square brackets and in that array,
you can have any data you could normally use too,
you can use strings in there like sports and cooking,
you can have numbers in there and you don't have to use one and the same type in the array,
here we are mixing text and numbers,
you could have arrays with just text, just numbers and so on
too of course. You can use boolean values and you can even store objects in there or other arrays,
that is all possible.
Now here I'll have an array of text though
and you can use for loops to go through that with this syntax for example, with the for of loop where we
store each element for each iteration in that hobby variable
and now if we do that, we would execute console log two times because we have two elements and we're looping
through all of them and I'm outputting the current value we're currently looking at because this will
change for every iteration going through it from left to right, we'll output that for every two iteration.
So now if I run this, we see sports and cooking printed in two lines because this executes two times.
So these are arrays,
the for of loop is interesting and it's also interesting that in javascript, we got a lot of built-in
methods we can use on arrays.
So on hobbies on that array,
if I add a dot, my IDE suggests a lot of methods I can use on arrays in javascript and all these methods
help me go through the elements in the array, manipulate them, get a subset of that array, whatever I need.
Often you'll see map for example which allows you to transform an array or transform the values and
map will actually return a new array,
so it will not edit the old one but give you a new one
and we can print that new one here actually
and just to show that the old one was not edited,
we can print that right below
and now map always takes a function where you define how to edit that array or how to edit the elements
in the array.
That function will be executed on every element in the array one after another and you return the updated
version of the element.
So here we would get our hobby,
you can name this however you want and here I'm using an arrow function with one argument only
hence no parentheses and in here, I'll return the edited version,
for example here I could take my old hobby string and simply add hobby
and a whitespace in front of that.
So I'm simply constructing a new string which keeps the old hobby name but adds hobby colon whitespace
in front of it.
And yes since we only got one statement in that arrow function with a return statement, we can get
rid of the curly braces,
get rid of return and just return it like this,
this would be the equivalent.
And now if I clear this and I run play.js, I see the old array was not edited,
that is my second output here,
it's coming from this console log where I console logged the original array
but the result of my map here is a new array where I have my edited items with hobby added in front
of every item.
And this is something you'll see me do quite a bit in this course,
use that map method
and as I mentioned, it's only one of the many methods provided here,
always check out the docs on MDN to learn more about all these methods,
link can be found attached to this video and make sure that you understand how they work or that when
we use one of them in this course, you can quickly look that up in case it's not clear what exactly that
does, though of course I will do my best to explain it when we use it.
But these are arrays, very important data structures and some very important methods you can use on arrays.