JavaScript: Cannot manipulate array produced with string.match -
i have produced array using:
var arr = []; arr = string.match(/(?:^| )([a-z]+)(?= [a-z])/g);
this works expected , array full , can seen using console.log or alert().
the array consists of words need filter, trying use .splice remove unwanted instances of same word using:
for (var = arr.length - 1; >= 0; i--) { if (arr[i] === 'jim') { arr.splice(i, 1); } }
the loop doesn't recognize instances of, instance, 'jim' in array although there several.
i tried loop using array made myself , worked fine, ie:
arr = ['jim', 'bob', 'arthur', 'jim', 'fred']
i have tried following reports 'jim' !== 'jim' other names not equalling 'jim'. again loop works fine self assigned array.
var = arr.length; while ( i-- ) if (arr[i] === 'jim') arr.splice(i, 1); else alert( "'" + arr[i].tostring() + "' !== 'jim'" );
what array produced string.match not understanding? appreciated.
you can save lot of time using array.filter()
:
arr = arr.filter(function(x){ return x.trim() !== 'jim'; });
Comments
Post a Comment