splice
We know how to find positions in our array but the next missing step is knowing how to add or remove elements deep in the middle of our array. We are going to use the splice method; One of the most handy methods of the Array class.
Video Transcript
00:00How do we actually modify, change, remove, or add things
00:04into the middle of our array, now
00:05that we know how to find things in the middle of the array?
00:08Let's see how could we remove things.
00:10So first of all, let's just meet this new method.
00:13And this method is called splice.
00:16Splice is a very, very useful method.
00:17It enables you both to add and remove things from an array.
00:21So what I want to do right now, let
00:22me just scroll up so we could see our arrays more easily.
00:26Let's say we want to get rid of Chris.
00:28Let's see how we could get rid of Chris.
00:30It's quite very simple.
00:32A users dot splice.
00:36Now splice requires a few parameters.
00:39The first parameter is where do we want to start?
00:43We want to start from position two
00:45because that's where Chris is at.
00:46So I'm going to type two.
00:48And the second thing that we want
00:50to tell it how many items do we want to remove.
00:53And we want to remove only one item.
00:55So our first item told us that what position
00:58we're at position two, which Chris is at.
01:00And we want to delete one item, which is only Chris.
01:03If we wanted to delete more than that,
01:04we could have typed two and then delete Chris and Daffy.
01:07But in our scenario, we just want to delete Chris.
01:09If I close our bracket right now
01:11and we trace out our A users, let's see what happened.
01:19And as you can see, Chris does not exist anymore.
01:23And let's see what would happen if we would put two.
01:26If we would put two here, what would happen?
01:28We would still start from position two,
01:30but now we would get rid of Daffy as well.
01:34Okay, excellent.
01:36Now, what would happen if we just reverted that?
01:38What would happen if we wanted to remove Chris,
01:40but instead of not only removing him,
01:42we want to switch him with someone else
01:44or we want to switch him with multiple other users.
01:47For example, let's say we want to add here,
01:49we want to add here Ben and we want to add here Sharon.
01:55Okay, so what have I done here?
01:56I basically, first parameter is our index.
01:59Second parameter is how much do we want to delete?
02:02Third parameter and all others.
02:04So we could add here as many as we want.
02:06We could add Sharon, we could add here Fish,
02:09we could add here,
02:13Yam, and we could add here Jerry.
02:17Really doesn't matter.
02:18We could add as many as we want.
02:19If we run our application one more time,
02:21now look what would happen.
02:22So first of all, we definitely got rid of Chris and Daffy.
02:27And we've added all of these items
02:30before we got back to our Daffy and our.
02:34On the other hand, let's see what would happen
02:35if we didn't want to get rid of anyone.
02:37We just wanted to add someone in a specific position.
02:40Let's say we want to add someone in position number two,
02:43but we don't want to get rid of anyone.
02:45Let's see how we do that.
02:46A user's splice.
02:48And then again, we say position two,
02:50but in this time we say we don't want to delete anyone.
02:53And instead of deleting, we want to add Jerry.
02:57And let's see what would happen.
03:00And here we go.
03:01Jerry is now in position two
03:02and all the rest were pushed.
03:05And Chris was pushed as well
03:06because we did not delete him.
03:07Perfect, so splice basically enables us
03:09to do everything we really wanted to do with our array.
03:14It enables us to remove things from the middle,
03:16it enables us to add things,
03:18and all in one function.
03:20Now the beautiful thing about the splice
03:22is that it kind of like connects all the worlds together.
03:25And the last thing that we're really missing here
03:27is using what we learned in our last video.
03:30So let's see how we could actually do that.
03:32Let's just delete everything here
03:33and let's create a method
03:35that actually would combine all the two.
03:37So let's say we wanted to create a function
03:39that would call remove user.
03:42It would accept only a string,
03:44which would be user name,
03:47which would be a string.
03:49And it would return nothing.
03:54And again, don't forget to close your bracket,
03:57although CS5 closes it automatically for me.
04:01So now let's create our remove user.
04:03First of all, we wanna know what location our user is.
04:06And actually let's change our remove user
04:08to actually return a Boolean
04:09because maybe that user doesn't exist.
04:11So maybe we wanna send a value back
04:13telling the user whoever's removing it
04:16if they succeeded or not.
04:18So the first thing is we wanna find out
04:21our index of our user, right?
04:22So our index is gonna be an integer
04:25and we're just gonna ask our A users dot index of user name,
04:33we're gonna get like that our user name.
04:35Now let's ask a question.
04:37If index not equals minus one,
04:41because we said if the name does not exist,
04:44it would return minus one.
04:46In that case, we wanna do one thing.
04:49And the other case, we would wanna return a value of false.
04:54Right, we want our function to return false
04:56because there is no match.
05:00On the other hand, if there was a match,
05:01let's see what we do next.
05:03If there was a match, we wanna get rid of that user.
05:06How do we get rid of the user?
05:08We could use the splice, which we've met earlier.
05:11So A users dot splice from what position?
05:16From our index.
05:18How many items do you wanna delete?
05:19Only one, we don't really wanna add anything, done.
05:24Let's test out our application.
05:26Well, one thing we're missing
05:27is we wanna return here, true, right?
05:29Because we did remove and our function expects
05:32to get something in return.
05:35So what we usually would do
05:37is we wouldn't wanna put many return values.
05:39Sometimes I do that, sometimes many developers do that,
05:42but the right way to do it
05:44is we would create here a variable
05:46and we would set it to be is removed,
05:49which would be a Boolean.
05:51And we would set the value of our index question
05:55directly into our variable.
05:57Then we wouldn't need to create all this.
06:01We wouldn't even need our else statement.
06:04All that would be redundant.
06:06All we would have to do is return our is removed.
06:10Okay, so basically what I've done right here
06:12is exactly the same thing
06:13as what we've done a second ago.
06:16Only a little bit cleaner.
06:17So let me just copy this
06:20just so you could have both options.
06:22And I'm just gonna undo everything.
06:25Just bring it back to the way it was before we change this.
06:28And I'm just gonna create here,
06:30gonna change the name to remove user two.
06:34And we're gonna create here another function
06:37and we're just gonna leave it the way it was.
06:39So here we go.
06:40So we have our newer version right here.
06:42So we have our remove user
06:43and you could always refer back to remove user two
06:45and see how they're basically the same thing.
06:48So let's call our remove user.
06:50And let's start off by calling remove user,
06:52but let's send it a user that doesn't exist
06:54such as Ben in uppercase
06:57because Ben in uppercase does not exist.
06:59Now let's trace this out
07:00and just see what value comes back.
07:05And we see that a value of false comes back
07:07because there is no such user as Ben.
07:10And let's type Ben in the correct way,
07:13which is all lowercase.
07:15And we will see that it will come back, it's true.
07:18So it definitely found me, it found Ben.
07:21So now let's just confirm that everything was removed.
07:23So let's just trace out our A users.
07:28And here we go, Ben was removed from our array.
07:31Only thing we have to make sure
07:32is we wanna remove O to our A user level, right?
07:35So we wanna make sure that not only remove our A users,
07:38that we remove our A user level.
07:42Splice.
07:43And just for the sake of making sure
07:46that both methods do the same thing,
07:48I'm just gonna copy this,
07:49go down to this little function, paste it inside.
07:52So we'll do the same thing.
07:54And all I really did here is just I made sure
07:55that we're consistently keeping our methods.
07:59Because if not, we could have removed the user
08:01and the correlation between our arrays within break.
08:05Okay, so we learned how to use our splice to remove items.
08:09Let's see, how could we use our splice to add users?
08:12Okay, so let's create one more function
08:13and we've done this before
08:15and let's just call it add user at Dan.
08:20Now obviously, probably add user at Dan would not make sense
08:23because we already have the function push and unshift.
08:27So we could have used unshift to do that.
08:29But let's say we didn't wanna or didn't notice
08:33that Dan was actually the first item.
08:35So we're sending Dan,
08:36we're gonna tell it that we wanna put a new user
08:38which is called Ben.
08:39We actually wanna make sure
08:40that Ben has all rights on this site.
08:43Let's see, what would our outcome be?
08:45Hopefully, we're gonna get the exact same thing.
08:48And let's just trace out our a user level
08:53to make sure that actually nothing changed.
08:55Because if everything works the way it should be working,
08:58we removed Ben in line 10.
09:00But in line 11, we should have returned everything
09:03back to normal.
09:06And we have a little syntax error.
09:07Let's see what it is.
09:08We're just gonna double click on it.
09:10And I see that I have here some sort of error.
09:12So let's see what's happening here.
09:13It's saying expecting right paren before semicolon.
09:17So it's expecting a right paren before the semicolon.
09:21There we go.
09:22And that is true because this paren
09:24only closed for the add user at.
09:26And now we have this one for our trace.
09:30And here we go.
09:31First of all, it tells us that we removed
09:33and then we added back.
09:34And let's see Ben Dan Chris Daffy Art,
09:37Ben Dan Chris Daffy Art.
09:39True, false, false, true, true.
09:41True, false, false, true, true.
09:43And here we go.
09:44We're back exactly to the way we were in the beginning.
