Testing against a string/object
<p>I have a JSON-encoded PHP array:</p>

<pre><code>&lt;script&gt;
var registeredEmails = &lt;?php echo(json_encode($emails)); ?&gt;;
&lt;/script&gt;
</code></pre>

<p>To check that this works, I do this:</p>

<pre><code>console.log(registeredEmails);
// this outputs: ["john@domain.com", "mary@domain.com"]
</code></pre>

<p>And now I would like to iterate through that JSON and test a certain string against all the strings it contains.</p>

<pre><code>for (var email in registeredEmails) {
    if (registeredEmails.hasOwnProperty(email)) {

        var duplicate = registeredEmails[email];

        console.log(duplicate + ' is typeof: ' + typeof(duplicate));
        // this outputs: john@domain.com is typeof: string

        //  $(this).val() is a string from somewhere else
        if (duplicate.test($(this).val())) {
            // we found a match
        };
    }
}
</code></pre>

<p>As I understand it, the <code>test()</code> method tests for matches on strings. I've tested to make sure that my variable <code>duplicate</code> is a string, but apparently it's still an object. I'm given the following JS error:</p>

<pre><code>Uncaught TypeError: Object john@domain.com has no method 'test'
</code></pre>

<p>Why is that?</p>
