Adding AND Removing Elements in the Middle

JavaScript for Beginners - Arrays: Adding and Removing Elements Part II - Working with the middle of a JavaScript array
2 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€67.87
List Price:  €96.96
You save:  €29.09
£56.33
List Price:  £80.48
You save:  £24.14
CA$101.11
List Price:  CA$144.46
You save:  CA$43.34
A$112.90
List Price:  A$161.30
You save:  A$48.39
S$95.89
List Price:  S$136.99
You save:  S$41.10
HK$544.41
List Price:  HK$777.77
You save:  HK$233.35
CHF 63.59
List Price:  CHF 90.85
You save:  CHF 27.25
NOK kr795.45
List Price:  NOK kr1,136.41
You save:  NOK kr340.95
DKK kr506.37
List Price:  DKK kr723.41
You save:  DKK kr217.04
NZ$125.48
List Price:  NZ$179.27
You save:  NZ$53.78
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,489.45
List Price:  ৳12,128.31
You save:  ৳3,638.85
₹6,003.32
List Price:  ₹8,576.54
You save:  ₹2,573.22
RM314.95
List Price:  RM449.95
You save:  RM135
₦108,169.08
List Price:  ₦154,533.88
You save:  ₦46,364.80
₨19,460.08
List Price:  ₨27,801.31
You save:  ₨8,341.22
฿2,414.58
List Price:  ฿3,449.55
You save:  ฿1,034.97
₺2,476.26
List Price:  ₺3,537.66
You save:  ₺1,061.40
B$432.69
List Price:  B$618.15
You save:  B$185.46
R1,309.65
List Price:  R1,871.01
You save:  R561.36
Лв132.94
List Price:  Лв189.93
You save:  Лв56.98
₩102,666.23
List Price:  ₩146,672.33
You save:  ₩44,006.10
₪255.59
List Price:  ₪365.15
You save:  ₪109.55
₱4,073.94
List Price:  ₱5,820.16
You save:  ₱1,746.22
¥11,002.07
List Price:  ¥15,717.92
You save:  ¥4,715.85
MX$1,443.99
List Price:  MX$2,062.93
You save:  MX$618.94
QR254.73
List Price:  QR363.91
You save:  QR109.18
P971.15
List Price:  P1,387.42
You save:  P416.26
KSh9,031.62
List Price:  KSh12,902.87
You save:  KSh3,871.24
E£3,551.78
List Price:  E£5,074.19
You save:  E£1,522.41
ብር8,922.62
List Price:  ብር12,747.15
You save:  ብር3,824.52
Kz63,830.88
List Price:  Kz91,190.88
You save:  Kz27,360
CLP$70,211.88
List Price:  CLP$100,306.99
You save:  CLP$30,095.10
CN¥512.36
List Price:  CN¥731.98
You save:  CN¥219.61
RD$4,268.13
List Price:  RD$6,097.60
You save:  RD$1,829.46
DA9,503.22
List Price:  DA13,576.62
You save:  DA4,073.39
FJ$162.81
List Price:  FJ$232.59
You save:  FJ$69.78
Q539.11
List Price:  Q770.19
You save:  Q231.08
GY$14,612.09
List Price:  GY$20,875.31
You save:  GY$6,263.22
ISK kr9,750.89
List Price:  ISK kr13,930.45
You save:  ISK kr4,179.55
DH704.78
List Price:  DH1,006.87
You save:  DH302.09
L1,300.73
List Price:  L1,858.26
You save:  L557.53
ден4,174.94
List Price:  ден5,964.46
You save:  ден1,789.51
MOP$559.86
List Price:  MOP$799.84
You save:  MOP$239.97
N$1,311.40
List Price:  N$1,873.51
You save:  N$562.11
C$2,571.06
List Price:  C$3,673.10
You save:  C$1,102.04
रु9,593.53
List Price:  रु13,705.64
You save:  रु4,112.10
S/262.27
List Price:  S/374.69
You save:  S/112.42
K279.90
List Price:  K399.88
You save:  K119.97
SAR262.85
List Price:  SAR375.52
You save:  SAR112.66
ZK1,946.04
List Price:  ZK2,780.17
You save:  ZK834.13
L337.70
List Price:  L482.45
You save:  L144.75
Kč1,708.24
List Price:  Kč2,440.45
You save:  Kč732.21
Ft28,222.76
List Price:  Ft40,319.96
You save:  Ft12,097.20
SEK kr777.78
List Price:  SEK kr1,111.16
You save:  SEK kr333.38
ARS$72,264.67
List Price:  ARS$103,239.67
You save:  ARS$30,975
Bs482.86
List Price:  Bs689.83
You save:  Bs206.97
COP$305,718.10
List Price:  COP$436,758.87
You save:  COP$131,040.76
₡35,617.57
List Price:  ₡50,884.43
You save:  ₡15,266.85
L1,775.85
List Price:  L2,537.04
You save:  L761.19
₲546,995.01
List Price:  ₲781,454.93
You save:  ₲234,459.92
$U3,074.18
List Price:  $U4,391.87
You save:  $U1,317.69
zł289.95
List Price:  zł414.23
You save:  zł124.28
Already have an account? Log In

Transcript

Okay, so now we'll talk about kind of killing two birds with one stone, we're going to remove elements and add elements at the same time. So once again, back to the full array with five elements. And then here, I'm going to call the splice method and pass it the first organism one. So I'm saying start at position one, or letter B, the letter B is remove one element, so remove B, and then insert one element, which is the letter X. So what I should get is an array back with one element, which is B, which is the element that was removed. And then who should be now a x C, D. So I get the array back with one element.

And then when I inspect foo, I get a x C, D and E. So this is basically like a call. It's like a replace. It's a great way to replace any element in array. I could have said, let's see, we'll start over and we'll say foo equals this. And then we could say, food supplies and I'm going to say position three, sufficient three is ABCD. ABCD it's the letter D, we're going to say, remove one and add the letters x and y.

So we should get an array back with one element, which should be the letter D. And then we should be foo should become a, b, c x, y and E. I'll do that I get the letter D back and then when I inspect foo, I get ABC, x, y and D. So we did a replace, but we replaced one letter with two letters. And we could keep going, we could, we could, we could have inserted you know, 15 elements there. But the main thing to keep in mind is that with the splice method, you can can remove and insert elements at the same time. The first argument is the index where to start the second argument is the number of elements to remove and then the third argument or fourth or fifth and so on, is the element. You'd like to add and you can do that in one shot.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.