I want to implement a kind of jQuery live search. But before sending the input to the server I'd like to remove all items in my array which have 3 or less characters (because in the german language, those words usually can be ignored in terms of searching) So ["this", "is", "a", "test"]
becomes ["this", "test"]
$(document).ready(function() { var timer, searchInput; $('#searchFAQ').keyup(function() { clearTimeout(timer); timer = setTimeout(function() { searchInput = $('#searchFAQ').val().match(/\w+/g); if(searchInput) { for (var elem in searchInput) { if (searchInput[elem].length < 4) { //remove those entries searchInput.splice(elem, 1); } } $('#output').text(searchInput); //ajax call here } }, 500); }); });
Now my problem is that not all items get removed in my for loop. If I for example typ "this is a test" "is" gets removed, "a" stays. JSFIDDLE
I think the problem is the for loop because the indexes of the array change if I remove an item with splice, so it goes on with the "wrong" index.
Perhaps anybody could help me out?
Find the index of the array element you want to remove using indexOf , and then remove that index with splice . The splice() method changes the contents of an array by removing existing elements and/or adding new elements. The second parameter of splice is the number of elements to remove.
We can remove duplicate element in an array by 2 ways: using temporary array or using separate index. To remove the duplicate element from array, the array must be in sorted order. If array is not sorted, you can sort it by calling Arrays. sort(arr) method.
To remove duplicates from an array: First, convert an array of duplicates to a Set . The new Set will implicitly remove duplicate elements. Then, convert the set back to an array.
The only way to remove duplicates with a single loop is with a hashset or equivalent. Sorting the list first also works, but technically sorting involves many loops. calloc a status bit array, check if previously found and mark off. If the next value exceeds the range, realloc and clear the new elements.
You can loop backwards, with something like the following:
var searchInput, i; searchInput = ["this", "is", "a", "test"]; i = searchInput.length; while (i--) { if (searchInput[i].length < 4) { searchInput.splice(i, 1); } }
DEMO: http://jsfiddle.net/KXMeR/
This is because iterating incrementally through the array, when you splice it, the array is modified in place, so the items are "shifted" and you end up skipping the iteration of some. Looping backwards (with a while
or even a for
loop) fixes this because you're not looping in the direction you're splicing.
At the same time, it's usually faster to generate a new array instead of modifying one in place. Here's an example:
var searchInput, newSearchInput, i, j, cur; searchInput = ["this", "is", "a", "test"]; newSearchInput = []; for (i = 0, j = searchInput.length; i < j; i++) { cur = searchInput[i]; if (cur.length > 3) { newSearchInput.push(cur); } }
where newSearchInput
will only contain valid length items, and you still have the original items in searchInput
.
DEMO: http://jsfiddle.net/RYAx2/
In addition to the second solution above, a similar, newer Array.prototype
method is available to handle that better: filter
. Here's an example:
var searchInput, newSearchInput; searchInput = ["this", "is", "a", "test"]; newSearchInput = searchInput.filter(function (value, index, array) { return (value.length > 3); });
DEMO: http://jsfiddle.net/qky7D/
References:
Array.prototype.filter
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter Array.prototype.filter
browser support - http://kangax.github.io/es5-compat-table/#Array.prototype.filter If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With