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
€60.73
List Price:  €86.76
You save:  €26.03
£52.01
List Price:  £74.30
You save:  £22.29
CA$96.11
List Price:  CA$137.31
You save:  CA$41.19
A$108.19
List Price:  A$154.56
You save:  A$46.37
S$90
List Price:  S$128.58
You save:  S$38.58
HK$549.43
List Price:  HK$784.94
You save:  HK$235.50
CHF 57.24
List Price:  CHF 81.78
You save:  CHF 24.53
NOK kr707.20
List Price:  NOK kr1,010.33
You save:  NOK kr303.12
DKK kr453.08
List Price:  DKK kr647.29
You save:  DKK kr194.20
NZ$117.18
List Price:  NZ$167.41
You save:  NZ$50.23
د.إ257.03
List Price:  د.إ367.21
You save:  د.إ110.17
৳8,564.54
List Price:  ৳12,235.59
You save:  ৳3,671.04
₹6,061.24
List Price:  ₹8,659.29
You save:  ₹2,598.04
RM297.63
List Price:  RM425.20
You save:  RM127.57
₦108,492.19
List Price:  ₦154,995.49
You save:  ₦46,503.30
₨19,870.74
List Price:  ₨28,388
You save:  ₨8,517.25
฿2,293.95
List Price:  ฿3,277.22
You save:  ฿983.26
₺2,776.72
List Price:  ₺3,966.92
You save:  ₺1,190.19
B$385.84
List Price:  B$551.22
You save:  B$165.38
R1,299.90
List Price:  R1,857.08
You save:  R557.18
Лв118.71
List Price:  Лв169.60
You save:  Лв50.88
₩96,107.73
List Price:  ₩137,302.64
You save:  ₩41,194.91
₪243.62
List Price:  ₪348.05
You save:  ₪104.42
₱4,001.46
List Price:  ₱5,716.62
You save:  ₱1,715.15
¥10,225.88
List Price:  ¥14,609.03
You save:  ¥4,383.15
MX$1,342
List Price:  MX$1,917.22
You save:  MX$575.22
QR255.41
List Price:  QR364.88
You save:  QR109.47
P943.85
List Price:  P1,348.42
You save:  P404.56
KSh9,042.70
List Price:  KSh12,918.70
You save:  KSh3,876
E£3,542.39
List Price:  E£5,060.78
You save:  E£1,518.38
ብር9,629.95
List Price:  ብር13,757.66
You save:  ብር4,127.71
Kz63,827.73
List Price:  Kz91,186.38
You save:  Kz27,358.65
CLP$65,871.78
List Price:  CLP$94,106.58
You save:  CLP$28,234.80
CN¥502.52
List Price:  CN¥717.91
You save:  CN¥215.39
RD$4,148.93
List Price:  RD$5,927.29
You save:  RD$1,778.36
DA9,117.43
List Price:  DA13,025.47
You save:  DA3,908.03
FJ$158.17
List Price:  FJ$225.97
You save:  FJ$67.79
Q538.22
List Price:  Q768.93
You save:  Q230.70
GY$14,651
List Price:  GY$20,930.90
You save:  GY$6,279.89
ISK kr8,658.05
List Price:  ISK kr12,369.18
You save:  ISK kr3,711.12
DH639.18
List Price:  DH913.16
You save:  DH273.97
L1,197.54
List Price:  L1,710.84
You save:  L513.30
ден3,739.17
List Price:  ден5,341.90
You save:  ден1,602.73
MOP$566.23
List Price:  MOP$808.93
You save:  MOP$242.70
N$1,264.38
List Price:  N$1,806.34
You save:  N$541.95
C$2,577.14
List Price:  C$3,681.79
You save:  C$1,104.64
रु9,700.04
List Price:  रु13,857.80
You save:  रु4,157.75
S/251.47
List Price:  S/359.26
You save:  S/107.78
K292.78
List Price:  K418.28
You save:  K125.49
SAR262.62
List Price:  SAR375.19
You save:  SAR112.56
ZK1,638.44
List Price:  ZK2,340.73
You save:  ZK702.28
L305.50
List Price:  L436.45
You save:  L130.95
Kč1,507.45
List Price:  Kč2,153.60
You save:  Kč646.14
Ft24,447.16
List Price:  Ft34,926.02
You save:  Ft10,478.85
SEK kr676.68
List Price:  SEK kr966.73
You save:  SEK kr290.04
ARS$81,411.45
List Price:  ARS$116,307.06
You save:  ARS$34,895.61
Bs483.89
List Price:  Bs691.30
You save:  Bs207.41
COP$285,769.93
List Price:  COP$408,260.26
You save:  COP$122,490.32
₡35,354.42
List Price:  ₡50,508.48
You save:  ₡15,154.06
L1,828.97
List Price:  L2,612.93
You save:  L783.95
₲558,948.33
List Price:  ₲798,531.84
You save:  ₲239,583.51
$U2,863.35
List Price:  $U4,090.68
You save:  $U1,227.32
zł259.98
List Price:  zł371.42
You save:  zł111.43
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.