3

I want to make a filter. Where if you input a word that is in the 'blacklist' it will tell something. I've got all the code but have a problem.

JS:

input = document.getElementById("input").value;
array = ["1","2","3"];
function filter() {
  if (input == array)
  // I will do something.
  } else {
  // Something too
  }
}

I want to make it so that if the input is a item in the array. That the statement is true. But what is the correct way to do this? Because what I'm doing here doesn't work! Also I want to get rid of the case sensitive! So that if the array has hello in it both hello and Hello are detected.

Sorry if this question is asked before. I searched for it but didn't know what keywords to use.


EDIT 1:

I am changing my question a little bit: I want to check what is in my original question but with some other features.

I also want to check if input has a part of an item in array. So that if the input is hello that helloworld is being detected because is has hello in it. As well as hello or Hello.

3
  • stackoverflow.com/questions/1181575/… Commented Jun 11, 2015 at 14:08
  • stackoverflow.com/questions/12623272/… Commented Jun 11, 2015 at 14:09
  • 1
    I think the keyword can be 'javascript array contains some item', 'javascript check item is in array' or 'javascript input is a item in the array(just copy your word)', it should not be hard to think if you know what you want.... Commented Jun 11, 2015 at 14:14

3 Answers 3

6

Use indexOf:

if (array.indexOf(input) > -1)

It will be -1 if the element is not contained within the array.

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

2 Comments

Thanks! Is there a way to get rid of the case sensitive, so that if I have hello. In the array both hello and Hello are detected?
@Finiox No probems, you'd have to use map for case insensitivity and map your values into a temporay array containing the uppercase values, then convert your input to uppercase and do the comparison. It's described in detail here: stackoverflow.com/questions/24718349/…
1

This code should work:

input = document.getElementById("input").value;
array = ["1","2","3"];
function filter() {
   if (array.indexOf(input) >= 0)
      // I will do something.
    } else {
      // Something too
    }
}

The indexOf Method is member of the array type and returns the index (beginning at 0) of the searched element or -1 if the element was not found.

1 Comment

The more commonly used is > -1. I would use this to avoid any further confusion for the OP
0

I think what you are looking for is

input = document.getElementById("input").value;
array = ["1","2","3"];
function filter() {
  if (array.indexOf(input) !== -1 )
  // I will do something.
  } else {
  // Something too
  }
}

2 Comments

If fore some reason indexOf returned undefined, or null, this will return a false positive
from the doc "This method returns -1 if the value to search for never occurs"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.