WEBVTT

00:00.240 --> 00:06.330
In this lesson I'm going to show you how you can add or remove one or more elements from your list not

00:06.330 --> 00:15.030
just from the end like we saw previously but from any position for both cases are we moving will be

00:15.030 --> 00:17.860
using a method called splice.

00:17.970 --> 00:19.920
I'll show you how this works.

00:19.950 --> 00:26.340
It's important to understand the concept of the index very well so knowing that the index started from

00:26.340 --> 00:31.790
zero and it goes all the way to the length of your list minus one.

00:31.800 --> 00:36.030
So in this case the length of my list to six there are six elements here.

00:36.030 --> 00:39.730
So the index will go from zero to five.

00:40.620 --> 00:43.520
Let's call this splice method.

00:44.130 --> 00:49.740
Now the first thing I need to enter is the position that index where this whole thing is going to happen.

00:49.740 --> 00:55.840
So let's start by adding new elements and then we'll remove you will remove elements afterwards.

00:56.010 --> 01:02.180
So I want to add two elements and I want them to be in here in position two.

01:02.340 --> 01:08.240
When I add elements to to position the elements that are currently in that position will basically.

01:08.250 --> 01:11.190
You can you can imagine this as being moved to the right.

01:11.220 --> 01:16.470
So if I add two more elements we'll have one two and then those two elements.

01:16.590 --> 01:18.630
And then the element that's in position to win.

01:18.710 --> 01:21.660
And all of the rest will be moved to the right.

01:21.690 --> 01:29.700
So I want to add stuff in position to as I said and the second parameter of splice has to do with how

01:29.700 --> 01:31.810
many elements are we're removing.

01:31.860 --> 01:33.560
Are not removing anything here.

01:33.570 --> 01:35.260
We're just adding stuff.

01:35.280 --> 01:41.940
So I'm going to type in the zero and now I just have to write all the elements I will be adding onto

01:41.940 --> 01:42.910
that position.

01:43.080 --> 01:49.980
So that's the 10 20 and 30 and

01:52.780 --> 01:57.400
let's look at that in our console.

01:59.070 --> 02:08.270
And you see that we've added 10 20 and 30 into position to all the previous elements were moved to the

02:08.270 --> 02:14.720
right as I said we can also just remove elements from certain positions.

02:14.960 --> 02:22.250
So imagine I want to get rid of those elements that I added so that those elements they are that they

02:22.250 --> 02:24.060
start in position to.

02:24.180 --> 02:26.150
And there's three of them.

02:26.270 --> 02:29.630
So I mean I do a splice starting position two.

02:29.870 --> 02:35.900
And I'm going to get rid of three elements and I won't add any new elements so I leave the rest blank.

02:36.350 --> 02:38.890
And we when to show that as well.

02:41.560 --> 02:49.110
And see how we got our original list back by removing three elements starting position to.

02:49.260 --> 02:55.500
You can do two operations two operations at the same time as well.

02:55.880 --> 03:02.360
So I imagine that I want to get rid of 4 and 5 and at the same time I want to add another number in

03:02.360 --> 03:13.280
there that starts all in position 3 and then I'm getting a read of two elements and I want to add two

03:13.280 --> 03:14.610
more elements.

03:19.430 --> 03:29.430
That will give me an array where these two elements are gone now and we added two new elements.

03:29.540 --> 03:37.490
Another way of referring to this to the position of the element is you can start from zero and go all

03:37.490 --> 03:39.710
the way to five in this case.

03:39.800 --> 03:45.220
But you can also do right minus one minus one is the last element.

03:45.230 --> 03:47.200
It's as if we were starting from the here.

03:47.210 --> 03:54.300
So it goes like this minus one or minus two minus three and so forth until minus the length of the string.

03:54.320 --> 03:56.620
In this case minus six.

03:56.720 --> 04:04.820
So there's just an alternative notation say that I want to add that I want to get rid of this one so

04:05.190 --> 04:08.520
that one would be in position minus two.

04:09.420 --> 04:17.260
And I want to get rid of that's just that single element.

04:17.560 --> 04:21.060
Well in this case minus 2 will be this one.

04:21.060 --> 04:23.780
Two hundred so I'm going to get a read of the two hundred.

04:26.400 --> 04:33.240
See that's how you you can refer to these positions used in the negative and not negative numbers as

04:33.240 --> 04:33.970
well.

04:34.410 --> 04:42.270
So I think this covers pretty much most cases are all cases where you need to remove stuff from a list

04:42.710 --> 04:44.750
and you need to add new elements from a list.
