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
€61.72
List Price:  €88.18
You save:  €26.45
£52.87
List Price:  £75.53
You save:  £22.66
CA$97.02
List Price:  CA$138.60
You save:  CA$41.58
A$110.27
List Price:  A$157.54
You save:  A$47.26
S$92.13
List Price:  S$131.62
You save:  S$39.49
HK$542.98
List Price:  HK$775.73
You save:  HK$232.74
CHF 57.10
List Price:  CHF 81.58
You save:  CHF 24.47
NOK kr739.96
List Price:  NOK kr1,057.13
You save:  NOK kr317.17
DKK kr460.81
List Price:  DKK kr658.33
You save:  DKK kr197.52
NZ$118.41
List Price:  NZ$169.17
You save:  NZ$50.75
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,503.56
List Price:  ৳12,148.47
You save:  ৳3,644.90
₹6,001.73
List Price:  ₹8,574.28
You save:  ₹2,572.54
RM308.83
List Price:  RM441.20
You save:  RM132.37
₦112,312.95
List Price:  ₦160,453.95
You save:  ₦48,141
₨19,634.70
List Price:  ₨28,050.78
You save:  ₨8,416.07
฿2,347.21
List Price:  ฿3,353.31
You save:  ฿1,006.09
₺2,666.14
List Price:  ₺3,808.93
You save:  ₺1,142.79
B$409.76
List Price:  B$585.40
You save:  B$175.63
R1,319.88
List Price:  R1,885.63
You save:  R565.74
Лв120.61
List Price:  Лв172.31
You save:  Лв51.69
₩99,997.86
List Price:  ₩142,860.21
You save:  ₩42,862.35
₪258.61
List Price:  ₪369.46
You save:  ₪110.85
₱3,963.98
List Price:  ₱5,663.08
You save:  ₱1,699.09
¥10,003.81
List Price:  ¥14,291.77
You save:  ¥4,287.96
MX$1,403.60
List Price:  MX$2,005.23
You save:  MX$601.63
QR255.13
List Price:  QR364.48
You save:  QR109.35
P966.02
List Price:  P1,380.09
You save:  P414.06
KSh9,074.20
List Price:  KSh12,963.70
You save:  KSh3,889.50
E£3,566.08
List Price:  E£5,094.62
You save:  E£1,528.54
ብር9,098.70
List Price:  ብር12,998.70
You save:  ብር3,900
Kz64,110.84
List Price:  Kz91,590.84
You save:  Kz27,480
CLP$67,834.30
List Price:  CLP$96,910.30
You save:  CLP$29,076
CN¥512.09
List Price:  CN¥731.59
You save:  CN¥219.50
RD$4,308.32
List Price:  RD$6,155.01
You save:  RD$1,846.68
DA9,289.14
List Price:  DA13,270.77
You save:  DA3,981.63
FJ$160.53
List Price:  FJ$229.34
You save:  FJ$68.80
Q539.80
List Price:  Q771.17
You save:  Q231.37
GY$14,685.30
List Price:  GY$20,979.91
You save:  GY$6,294.60
ISK kr8,969.21
List Price:  ISK kr12,813.71
You save:  ISK kr3,844.50
DH648.63
List Price:  DH926.65
You save:  DH278.02
L1,209.12
List Price:  L1,727.39
You save:  L518.27
ден3,796.75
List Price:  ден5,424.17
You save:  ден1,627.41
MOP$559.06
List Price:  MOP$798.69
You save:  MOP$239.63
N$1,360.25
List Price:  N$1,943.30
You save:  N$583.05
C$2,575.77
List Price:  C$3,679.83
You save:  C$1,104.06
रु9,627.93
List Price:  रु13,754.78
You save:  रु4,126.84
S/261.03
List Price:  S/372.92
You save:  S/111.88
K289.22
List Price:  K413.20
You save:  K123.97
SAR262.63
List Price:  SAR375.20
You save:  SAR112.57
ZK1,982.38
List Price:  ZK2,832.09
You save:  ZK849.71
L307.18
List Price:  L438.85
You save:  L131.67
Kč1,547.17
List Price:  Kč2,210.34
You save:  Kč663.17
Ft25,296.99
List Price:  Ft36,140.11
You save:  Ft10,843.11
SEK kr683.50
List Price:  SEK kr976.47
You save:  SEK kr292.97
ARS$83,728.73
List Price:  ARS$119,617.60
You save:  ARS$35,888.87
Bs485.34
List Price:  Bs693.38
You save:  Bs208.03
COP$300,546.89
List Price:  COP$429,371.10
You save:  COP$128,824.21
₡35,904.74
List Price:  ₡51,294.69
You save:  ₡15,389.94
L1,814.10
List Price:  L2,591.69
You save:  L777.58
₲559,963.15
List Price:  ₲799,981.65
You save:  ₲240,018.49
$U3,006.84
List Price:  $U4,295.68
You save:  $U1,288.83
zł264.45
List Price:  zł377.81
You save:  zł113.35
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.