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

4
  • 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. Commented May 18, 2016 at 15:35
  • You call splice() with index, which is the position of key1 in the string. It seems wrong to me. Commented May 18, 2016 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. Commented May 18, 2016 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. Commented May 18, 2016 at 15:51

1 Answer 1

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;
});
Sign up to request clarification or add additional context in comments.

6 Comments

Is Array#filter available in all browsers? How to check that it works depending on browser versions?
@A.L Browser Compatibility. To support older browsers the ES5 approach should be used.
@A.L all browsers including IE9+. IE8 and less it's not compatible with this specification
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 return el.indexOf(key1) !== -1; result is the array that you should be using.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.