0

I have this simple array

var array = ['x.89999', 'y.sisisis', 'x.585858'];

I want to remove all the items in the array starting by 'x.' so to return this array:

['y.sisisis']

How can i do this without having to loop or iterate all the entire array? (i know it's probably not possible so don't mind if not possible)

Is there some builtin / native code i can use?

Thanks

itsme
  • 45,343
  • 90
  • 210
  • 334
  • 2
    You can use `.filter()`, but that still loops internally. – Scimonster May 11 '15 at 10:30
  • Using regex is the obvious and probably the only answer, but you have to iterate over the elements in the array to do that. Or use `.filter` but that iterates over the array too. – Deepak Kamat May 11 '15 at 10:31
  • If you ensure the list is always sorted, you only have to iterate it until you encounter the first string that doesn't start with x. – Jan Sommer May 11 '15 at 10:33
  • Take a look in older post http://stackoverflow.com/questions/3954438/remove-item-from-array-by-value – Systematix Infotech May 11 '15 at 10:35
  • 1
    Sorry I posted a wrong link this may help you out http://stackoverflow.com/questions/18518855/remove-all-items-in-array-that-start-with-a-particular-string – Systematix Infotech May 11 '15 at 10:42
  • Let me get this straight. You want to eliminate all items that start with `x.`, but do so without actually looking at each item. Perhaps you could investigate quantum computing. –  May 11 '15 at 10:43

4 Answers4

5

you may use array.filter()

var newArray = array.filter(function(item){
    return item.indexOf('x.') !== 1;
});

there is no way to do this job without looping through the whole array. The only case – array is sorted alphabetically. But sorting demands looping through too

Katya Pavlenko
  • 2,865
  • 2
  • 12
  • 25
2

Assuming that the items to be removed will always precede the other items alphabetically (ie x is before y), you can sort your array and then break the loop as soon as the non-x value has been found:

function filter(arr) {
    arr.sort();
    for (var i = 0, l = arr.length; i < l; i++) {
        if (arr[i][0] !== 'x') {
            break;
        }
    }
    return arr.slice(i);
}

filter(arr); // logs: "Iterated to 5 of array length 9"

DEMO

Of course, as JazzCat mentions, this is no good if you only have one y value and it's right at the end of the array - you're still going to have to iterate over the whole array.

Andy
  • 39,764
  • 8
  • 53
  • 80
1

Alternate option is to use regular expression, something like this. Fix your regex according to your requirement.

var array = ['x.89999', 'y.sisisis', 'x.585858'];

var filtArr = array.filter(function(x){
    return x.match("^[x].+$");
});

console.log(filtArr);
Harish
  • 558
  • 4
  • 11
1

One different answer not using explicit iteration or Array.prototype.filter:

var array = ['x.89999', 'y.sisisis', 'x.585858'];
array = array.join("#").replace(/(#?x\.\w*)/g,"").replace(/^#/,"").split("#");
//["y.sisisis"]

Of course you will need to take care of the separator character.

The good point is that it works on old browsers (ie8) while Array.prototype.filter does not.

Jorgeblom
  • 2,728
  • 18
  • 22