Solved

JavaScript: Test string against each item in array

Posted on 2011-02-20
1
847 Views
Last Modified: 2012-05-11
I have a JavaScript array containing a list of blacklisted words.

I want to return true if a string contains any of the blacklisted words.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>

<script type="text/javascript">
/*<![CDATA[*/

Array.prototype.contains = function(obj) {
  var i = this.length;
  while (i--) {
    if (this[i] === obj) {
      return true;
    }
  }
  return false;
}

var badStuff = new Array("aaa","bbb","ccc","ddd","eee");

var v = 'aaa hello world';
alert(badStuff.contains(v)); // should alert true because v contains aaa and aaa is in the baddStuff array

var v = 'This is a bbbbbbbb test';
alert(badStuff.contains(v)); // should alert true because v contains bbb and bbb is in the baddStuff array

var v = 'Hello World';
alert(badStuff.contains(v)); // should alert false

var v = 'ccc';
alert(badStuff.contains(v)); // should alert true

/*]]>*/
</script>

</head>
<body>

<h1>Hello World</h1>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
1 Comment
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 34939158
You had a slight problem with the ===.  Use indexOf instead.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>

<script type="text/javascript">
/*<![CDATA[*/

Array.prototype.contains = function(obj) {
  var i = this.length;
  while (i--) {
    if (obj.indexOf(this[i]) != (-1))
    {
      return true;
    }
  }
  return false;
}

var badStuff = new Array("aaa","bbb","ccc","ddd","eee");

var v = 'aaa hello world';
alert(badStuff.contains(v)); // should alert true because v contains aaa and aaa is in the baddStuff array

var v = 'This is a bbbbbbbb test';
alert(badStuff.contains(v)); // should alert true because v contains bbb and bbb is in the baddStuff array

var v = 'Hello World';
alert(badStuff.contains(v)); // should alert false

var v = 'ccc';
alert(badStuff.contains(v)); // should alert true

/*]]>*/
</script>

</head>
<body>

<h1>Hello World</h1>

</body>
</html>

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now