Adding 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.76
List Price:  €88.24
You save:  €26.47
£52.47
List Price:  £74.97
You save:  £22.49
CA$96.68
List Price:  CA$138.12
You save:  CA$41.44
A$109.46
List Price:  A$156.38
You save:  A$46.92
S$91.56
List Price:  S$130.80
You save:  S$39.24
HK$542.83
List Price:  HK$775.51
You save:  HK$232.67
CHF 57.85
List Price:  CHF 82.65
You save:  CHF 24.79
NOK kr728.45
List Price:  NOK kr1,040.69
You save:  NOK kr312.23
DKK kr460.98
List Price:  DKK kr658.57
You save:  DKK kr197.59
NZ$118.03
List Price:  NZ$168.63
You save:  NZ$50.59
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.18
৳8,531.88
List Price:  ৳12,188.92
You save:  ৳3,657.04
₹5,921.90
List Price:  ₹8,460.22
You save:  ₹2,538.32
RM302
List Price:  RM431.45
You save:  RM129.45
₦112,424.23
List Price:  ₦160,612.93
You save:  ₦48,188.70
₨19,730.13
List Price:  ₨28,187.10
You save:  ₨8,456.97
฿2,342.56
List Price:  ฿3,346.66
You save:  ฿1,004.10
₺2,691.65
List Price:  ₺3,845.38
You save:  ₺1,153.73
B$398.65
List Price:  B$569.53
You save:  B$170.87
R1,305.11
List Price:  R1,864.53
You save:  R559.41
Лв120.84
List Price:  Лв172.64
You save:  Лв51.79
₩99,844.59
List Price:  ₩142,641.24
You save:  ₩42,796.65
₪253.27
List Price:  ₪361.83
You save:  ₪108.56
₱3,909.64
List Price:  ₱5,585.44
You save:  ₱1,675.80
¥10,098.99
List Price:  ¥14,427.75
You save:  ¥4,328.76
MX$1,374.71
List Price:  MX$1,963.95
You save:  MX$589.24
QR254.83
List Price:  QR364.06
You save:  QR109.23
P961.02
List Price:  P1,372.95
You save:  P411.92
KSh9,063.70
List Price:  KSh12,948.70
You save:  KSh3,885
E£3,556.37
List Price:  E£5,080.75
You save:  E£1,524.37
ብር9,421.13
List Price:  ብር13,459.34
You save:  ብር4,038.20
Kz63,830.88
List Price:  Kz91,190.88
You save:  Kz27,360
CLP$66,576.58
List Price:  CLP$95,113.48
You save:  CLP$28,536.90
CN¥510.57
List Price:  CN¥729.42
You save:  CN¥218.85
RD$4,131.65
List Price:  RD$5,902.61
You save:  RD$1,770.96
DA9,284.16
List Price:  DA13,263.65
You save:  DA3,979.49
FJ$158.13
List Price:  FJ$225.92
You save:  FJ$67.78
Q539.19
List Price:  Q770.30
You save:  Q231.11
GY$14,646.40
List Price:  GY$20,924.33
You save:  GY$6,277.92
ISK kr8,999.31
List Price:  ISK kr12,856.71
You save:  ISK kr3,857.40
DH648.26
List Price:  DH926.13
You save:  DH277.86
L1,205.03
List Price:  L1,721.55
You save:  L516.51
ден3,801.61
List Price:  ден5,431.11
You save:  ден1,629.49
MOP$560.81
List Price:  MOP$801.19
You save:  MOP$240.38
N$1,306.98
List Price:  N$1,867.19
You save:  N$560.21
C$2,583.28
List Price:  C$3,690.56
You save:  C$1,107.28
रु9,492.56
List Price:  रु13,561.38
You save:  रु4,068.82
S/257.39
List Price:  S/367.72
You save:  S/110.32
K286.62
List Price:  K409.48
You save:  K122.85
SAR262.47
List Price:  SAR374.98
You save:  SAR112.50
ZK1,953.40
List Price:  ZK2,790.70
You save:  ZK837.29
L307.47
List Price:  L439.26
You save:  L131.79
Kč1,538.82
List Price:  Kč2,198.41
You save:  Kč659.58
Ft24,960.28
List Price:  Ft35,659.07
You save:  Ft10,698.79
SEK kr679.32
List Price:  SEK kr970.50
You save:  SEK kr291.18
ARS$81,756.65
List Price:  ARS$116,800.22
You save:  ARS$35,043.57
Bs485.21
List Price:  Bs693.19
You save:  Bs207.97
COP$293,756.36
List Price:  COP$419,669.93
You save:  COP$125,913.57
₡35,459.16
List Price:  ₡50,658.12
You save:  ₡15,198.95
L1,821.77
List Price:  L2,602.65
You save:  L780.87
₲562,262.91
List Price:  ₲803,267.16
You save:  ₲241,004.24
$U2,936.85
List Price:  $U4,195.69
You save:  $U1,258.83
zł264.76
List Price:  zł378.25
You save:  zł113.48
Already have an account? Log In

Transcript

Let's talk about removing some elements. So once again, I'll define foo. And it now has five elements. And then when I call foo dot splice, I'm passing it here two arguments. The first argument is the position of the position in the array, I'd like to perform this operation, as I'm saying, starting at position three, which to us is position four, because it's zero base. So for example, letter A is position zero, B is one, C is two, and d is three.

So we're starting with letter D, and then we pass it a one as the second argument. The second argument is a number of elements we'd like to remove. So I'm saying starting at position three, which is really the letter D, remove one letter, or one element, which in this case will be d. so this will return an array with the with the letter D in it. And that's exactly what I get. I get an array which has one element, which is the letter D. And if I inspect foo, I see that foo is now missing D, we removed one element from the array. Let's look at Another example.

So once again, I'll define foo. And it's got five elements again. And then now I'm going to call splice and pass it two arguments, three and two. So I'm saying starting at the third index, or the letter D, remove two letters. So now I'm going to get an array back with two letters, I'm going to get an array with letters D, and E, and that's going to remove them from the array. So execute that.

And I do get D and E back. And when I inspect foo, I get ABC because I removed DNA. So the main point here is that the syntax for removing elements is the first argument is the position at which you'd like to start working and the second argument is a number of elements to remove. Now, there's one thing I like to point out about removing elements is that we can start from the end if we'd like. So here are redefined foo. And then I'm now I'm passing it a negative number I'm seeing starting to count backwards three from the end and remove two elements.

So if I count backwards three, I get a DC. So starting at C remove two elements, so I should get an array with two elements in it contain illiteracy and D. And then the array should be missing those two letters. So I'm going to execute that. I get the array back with two elements. And then when I inspect foo, I'll see that C and D are missing. We just have A, B and E. So we can start backwards from the ray by passing a negative number.

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.