Use sort to add a simple algorithm that does not loop through the entire array and delete some elements within the ARRAY.

Source: Internet
Author: User

yesterday, a scene like This: there is a non-paged list of goods, there may be thousands of data (and possibly static Data) and even more, there is a delete function, we need to delete some of these items.

My first reaction was that too much data could not loop through the entire array, only to get to their index, and then iterate over the index, using the Array's splice () method to delete it. And I did, but I found a fatal Bug.

The reason for the bug is This: I first got a set of indexes (the index of the product I need to delete), and then I loop through the set of indexes, and I execute the Splie () method every time I loop, but because the splice method executes, it changes the array, so my array length decreases by one But the index I got was not changed, causing my second splice to get a Bug.

These I thought of another method: the execution of splice index should also subtract "1", so I wrote an algorithm, assuming that all the indexes are "n", and they are in a positive order, then splice after the array minus "1", the corresponding "n" minus "1" ; splice after the second time, "n-2"; after the third time, "n-3";

When I discovered this rule, I thought about the index of the element that I needed to delete First. Here is an example:

var productitems = ["a", "b", "c", "d"];

var indexs = [1, 2, 3,];

The indexes that can be deleted normally are calculated first:

var newindexs = indexs.map (function (val, idx) {return val-idx; })

Because splice is a bug from the second time, the position of our first index is correct, starting with the second "n-1", which is exactly the index of the indexed array.

And then

Newindexs.foreach (function (index) {

Productitems[index].splice (index, 1)

})

Productitems ===> ["a"]

This is all based on the case that the indexed array is in a positive order, so the sort method is used before the index is evaluated;

This enables the entire array to be not looped, thus removing certain elements that are Specified. Avoid due to the data too large caused by the performance problem, after all, every deletion to cycle thousands of data, it is very scary!!!!

Use sort to add a simple algorithm that does not loop through the entire array and delete some elements within the ARRAY.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.