Learn JavaScript Episode #14: Arrays Part 3 [w/ subs]



00:00:00 – Let me clear this out before we start on the next video. Alright, so, what we're going
00:00:05 – to do in this video is learn how to get the index number of an array, and then
00:00:10 – how we can use that. So, I'm going to type hobbies, I'm going to store this in
00:00:16 – a variable. So, I'm going to type indexNum, and it's going to be equal to our
00:00:24 – array. So hobbies, actually let me refresh entirely here so we have the full
00:00:29 – hobbies list. What we're going to do is we're going to do is get the index number of this,
00:00:36 – so counting from 0 this will be 0, 1, 2. So, this has an index number of 2 in the
00:00:44 – array, reading. So what we're going to do is we're going to create the variable
00:00:49 – indexNum, it's going to be equal to
00:00:54 – hobbies.indexOf, and we're going to be looking for 'reading.' So now if I type
00:01:04 – indexNum it's going to return the number 2 because that is the index
00:01:09 – number 2 inside of the array. So how we remove items from an array specifically,
00:01:15 – so if we wanted to remove item at index number 5 you know we could do that as
00:01:20 – well. For right now, we're going to remove index number 2. So we're going to remove
00:01:24 – reading from hobbies, and how we're going to do that is we are going to type
00:01:28 – hobbies.splice. And now this method takes two parameters. The first one is
00:01:38 – the position that we start at, and so if we typed 2 here it's going to start at
00:01:46 – reading. So this is going to be the first one that it removes, and then we can remove
00:01:50 – however many numbers at that point. So if I wanted to remove just one, we're just
00:01:54 – going to remove a reading from this list, the two variables that it takes in is 2
00:01:58 – and 1. Now, let's pretend that I don't know that I'm removing index number 2. So
00:02:03 – I'm going to use our variable indexNum, and we're going to remove reading. Now if
00:02:08 – I type hobbies you'll see that that has been removed. Now,
00:02:12 – I'm going to remove gaming, programming, and music using the same methods. So
00:02:16 – hobbies.splice, I'm going to splice from position one, I'm going to remove three
00:02:24 – items. Now if I type hobbies we get blacksmithing and archery. This is now,
00:02:31 – these are now our hobbies. So, that's how to get started with arrays. Now
00:02:38 – there's one other practical use for index of, and that's to evaluate whether
00:02:44 – or not a certain item is in the array. So, let's go ahead and write an if statement,
00:02:51 – and I'm going to write this in here, and the if statement is going to be if
00:02:56 – hobbies.indexOf('reading') is greater than minus 1, that means reading is in
00:03:07 – the array. So I'm going to console.log “I like reading!” I'm going to remove that
00:03:15 – for each, and then we're going to test another. if(hobbies.indexOf
00:03:27 – let's say 'sports') is more than minus 1 that means sports is in the array, which
00:03:36 – it's not, so what we're going to do is else console.log(“not in array”)
00:03:45 – I'm going to save this and I'm going to refresh it, and we'll see I like reading
00:03:48 – and then not in array because this returns an indexed number that is 0 or
00:03:54 – greater. This can either be at index 0 or greater. If this is not found at all,
00:04:00 – the index of method is going to return the number minus 1. So, we could
00:04:05 – alternatively do this to test if something is not in the array, oops, we
00:04:11 – can say if it equals minus 1. I'm going to save it, I'm going to refresh, and it
00:04:18 – still says not in array. Now I used three equal signs here. This basically
00:04:24 – means not found, so we're checking if hobbies, sports,
00:04:27 – is not found. Now, we can do this with two, but that would
00:04:36 – return true if this was returned, right. So we're not comparing types, in that
00:04:42 – instance, we're comparing values, and certain values can be matched across
00:04:46 – types. For instance, true and true by using the two equal signs. So using three
00:04:50 – equal signs means does the value match and is it the same type, and so it's
00:04:55 – always safest to do this if you are looking for something specific in an
00:04:59 – application. If you're making any type of comparison that you know it needs to be
00:05:04 – one or the other, and it needs to be the same type, that's when you're going to
00:05:07 – use these. Alternatively, if it's not the same type you're going to use
00:05:11 – exclamation mark equals equals, and that's how we're going to work with that.
00:05:17 – So, I think we're all done with the arrays, next I would really like to move on to
00:05:21 – classes and building objects.


Video Url:
http://youtu.be/29HPnhcX4a4

Comments

comments