1

I have an array JavaScript and a string :

var pool1 = ['ca','cahier','cartable','carte','cartographe','canape'];
var key1 = 'car';

What I am trying to do is, remove from the array all value that aren't containing key1.

To do so I've written this function :

function searchInPool(key, pool){
    for (i = 0; i < pool.length; i++) {
        var index = pool[i].indexOf(key);
        if (index > -1) {
            pool.splice(index, 1);
        }
    }
    return pool;
}

It seems to be working, except that the final result gives me :

["cartable", "carte", "cartographe", "canape"]

It has succesfully removed caand cahier but canape shouldn't be here since it doesn't contain car anyone can explain me what I've misunderstood from what I've written in my function ?

The final result expected is :

["cartable", "carte", "cartographe"]

Thanks a lot

Nirnae
  • 1,310
  • 10
  • 22
  • 1
    You should reverse your loop `for (var i = pool.length - 1; i >= 0; i--) {` because this way you'll account for the shortening length of the array which isn't happening in your example. – Andy May 18 '16 at 15:35
  • You call `splice()` with `index`, which is the position of `key1` in the string. It seems wrong to me. – A.L May 18 '16 at 15:36
  • Since answer is closed, I can't post answer. But issues with your code are: 1) you change the array `pool` while you are looping over it, which gives your strange results and 2) the index you use to remove an item is the index where your code finds the string `car`, and it should be the index of the item in the array and 3) the check `(index > -1)` actually tries to remove items that DO have `car` in them. It is reallyt pure luck that the other two items are removed correctly and that the right items stay in. – wintvelt May 18 '16 at 15:48
  • 1
    @Barmar I would vote to reopen this question. OP's question is NOT how to remove item from array (so not duplicate), but "explain what I have misunderstood". Maybe the question is more suitable for Code Review than SO, but I do not think is duplicate. Maybe OP could weigh in too with his opinion. – wintvelt May 18 '16 at 15:51

1 Answers1

5

You can use Array#filter.

array.filter(e => !e.includes(key));

var pool1 = ['ca', 'cahier', 'cartable', 'carte', 'cartographe', 'canape'];
var key1 = 'car';

var result = pool1.filter(e => !e.includes(key1));
console.log(result);

ES5:

var result = pool1.filter(function(el) {
    return el.indexOf(key1) === -1;
});
Tushar
  • 78,625
  • 15
  • 134
  • 154
  • Is `Array#filter` available in all browsers? How to check that it works depending on browser versions? – A.L May 18 '16 at 15:38
  • 2
    @A.L [Browser Compatibility](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter#Browser_compatibility). To support older browsers the **ES5** approach should be used. – Tushar May 18 '16 at 15:39
  • @A.L all browsers including IE9+. IE8 and less it's not compatible with this specification – Marcos Pérez Gude May 18 '16 at 15:39
  • I dont seem to really understand he ES5 approach, it is working but gives the opposite of what I need and dont know how to reverse it :/ – Nirnae May 18 '16 at 16:00
  • @Nirnae `return el.indexOf(key1) !== -1;` `result` is the array that you should be using. – Tushar May 18 '16 at 16:01
  • @Nirnae: I recommend to read the documentation of `.filter` and `.indexOf`. `.filter` returns a new array only containing those elements for which the callback returns `true`. `.indexOf` returns the fist position of a substring inside a string, or `-1` if the string does not contain the substring. – Felix Kling May 18 '16 at 16:03