最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript removing and rearranging array elements - Stack Overflow

programmeradmin3浏览0评论

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)

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)

Share Improve this question edited Sep 26, 2012 at 4:30 KyelJmD asked Sep 26, 2012 at 4:18 KyelJmDKyelJmD 4,73210 gold badges58 silver badges78 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

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/en-US/docs/JavaScript/Reference/Global_Objects/Array/splice

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);

Simply, you can create a temporary array where you store the initial array elements you need and reassign the value of your initial array to the temporary array.

   function clean_array(my_array){
       var no_need_value = 'value you want to remove'
       var tmpArray = new Array()
       for (var i = 0; i < my_array.length; i++)
           if (my_array[i] != no_need_value)
               tmpArray.push(my_array[i])
       my_array = tmpeArray
   }
发布评论

评论列表(0)

  1. 暂无评论