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 trialDaiane Silva
5,668 PointsWell, I can remove items from the beginning and the end of my array, but If I want to remove the item in the middle?
var number = [1, 2, 3, 4] I want to remove number[2] but keep the rest, what should I do?
1 Answer
James Arroyo
645 Pointsvar indexToRemove= array.indexOf(2); -> this returns the index of number 2 Then remove it with splice: if (indexToRemove > -1) { array.splice(indexToRemove, 1); }
second param refers to the amount of elements you want to remove.
Daiane Silva
5,668 PointsDaiane Silva
5,668 PointsThanks!
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsAakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsWhy have you used the "conditional statement" here? In what conditions "indexOf()" can provide the negative index?