0

I have the following array:

etst,tset,tets,ttest,teest,tesst,testt,4est,test,dest

I want to delete the value of an input box from the array, here's what I'm trying:

var el = document.getElementById('searchInput').value; // this is "test"

var toSearchFor = eld.slice(0,10); // the array above

for(var i=0; i < toSearchFor.length; i++) {
   toSearchFor[i] = toSearchFor[i].replace(/el/g, "");
}

It's simply not replacing "test" with ""

How can I do that?

3 Answers 3

3

You can use Array.filter (see MDN) to filter out the desired value:

var arr = 'etst,tset,tets,ttest,teest,tesst,testt,4est,test,dest'.split(',')
   ,val = 'test'
   
document.querySelector('#result')
  .innerHTML = arr.filter(function (v) {return v != val});
<div id="result"></div>

A text field example in this jsFiddle

Sign up to request clarification or add additional context in comments.

2 Comments

This is great but if my array is as above 'toSearchFor' and I try toSearchFor.split(',') etc etc it fails saying toSearchFor.filter is not a function - example here jsfiddle.net/5uvbnpye
Hi @DarrenSweeney, maybe the jsfiddle @ jsfiddle.net/KooiInc/jfooLr3q clarifies things?
1

for global replacement of a string stored in a variable u need to create an instance of RegExp explicitly, like this:

var regex = new RegExp(el, "g");

then use it in replace function:

toSearchFor[i] = toSearchFor[i].replace(regex, "");

Comments

0

The problem with your code is in your regular expression: /el/g. This is trying to match the letters el, instead of whatever it's in the el variable. You could have done it using the RegExp construtor.

// ...
regexp = new RegExp(el); // No need to use 'g' here since you're looking for the whole word
toSearchFor[i] = toSearchFor[i].replace(regexp, "");
// ...

Here's another way of doing it:

var eld = ['etst','tset','tets','ttest','teest','tesst','testt','4est','test','dest'];
// var el = document.getElementById('searchInput').value;
var el = 'test';

console.log(eld);

var index = eld.indexOf(el);
if (index >= 0) {
    eld[index] = '';    
}

console.log(eld);

Here's the output:

["etst", "tset", "tets", "ttest", "teest", "tesst", "testt", "4est", "test", "dest"]
["etst", "tset", "tets", "ttest", "teest", "tesst", "testt", "4est", "", "dest"]

In this case, we're using Array.prototype.indexOf, which returns the first index at which a given element can be found in the array, so that we can access that element directly (if found).

I hope that helps!

2 Comments

If there are multiple instances of 'test' in the array this only strips out the first, is there a way to strip all out?
In that case, you would have to iterate through all of the elements of eld, as you were doing in the beginning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.