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 Arrays

Daiane Silva
Daiane Silva
5,668 Points

Well, 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

var 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.