Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript removing and rearranging array elements

I have this collection of images resources where that is stored in array, the user will select an image and then the selected image will be removed from the list(also from the array) and after that The array would be rearrange. How could I perform such task? (as much as possible I do not want to use an open source library)

like image 938
KyelJmD Avatar asked Jul 07 '26 03:07

KyelJmD


2 Answers

Sounds like you need to look up splice() method. It allows you to add and remove one to many items within an array at any index.

here's reference for it. https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/splice

like image 74
Lin Qiu Avatar answered Jul 08 '26 16:07

Lin Qiu


your question lacks a code example but you can use Array.splice(index,number) whereas index is zero based and number is how many items to remove.

images.splice(selectedIndex,1);
like image 43
ecco88 Avatar answered Jul 08 '26 16:07

ecco88