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 ca
and 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