Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Removing Items from an Array

jiwan gurung
jiwan gurung
4,248 Points

this didnt work but is it possible to do it this way?

this didnt work but is it possible to do it this way?

instead of the shift method can't you use the splice method to remove from one array and put it into a new array?

script.js
var orderQueue = ['1XT567437','1U7857317','1I9222528'];
var shipping = orderQueue.splice(0);
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

1 Answer

Jaspal Singh
Jaspal Singh
13,525 Points

hi Yes you can do but this is a specific challenge so you cannot do it here. you have to understand the difference as well between shift and splice methods.

  1. shift method --- The shift() method removes the first element from an array and returns that element. This method changes the length of the array. 2.splice method -- The splice() method changes the contents of an array by removing existing elements and/or adding new elements by providing the index value for example. var myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];

myFish.splice(2, 0, 'drum'); // insert 'drum' at 2-index position // myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]

myFish.splice(2, 1); // remove 1 item at 2-index position (that is, "drum") // myFish is ["angel", "clown", "mandarin", "sturgeon"]