In my possession, I have a PHP array that has been encoded in JSON format:
<script>
var registeredEmails = <?php echo(json_encode($emails)); ?>;
</script>
To verify that this is functioning properly, I conduct the following test:
console.log(registeredEmails);
// This results in: ["<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbd1d4d3d5fbdfd4d6dad2d595d8d4d6">[email protected]</a>", "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d101c0f043d1912101c1413531e1210">[email protected]</a>"]
My objective now is to iterate through the JSON data and compare a specific string against all the strings it contains.
for (var email in registeredEmails) {
if (registeredEmails.hasOwnProperty(email)) {
var duplicate = registeredEmails[email];
console.log(duplicate + ' is typeof: ' + typeof(duplicate));
// This shows: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0c666364624c6863616d6562226f6361">[email protected]</a> is typeof: string
// $(this).val() holds a string from another source
if (duplicate.test($(this).val())) {
// A match has been found
};
}
}
It appears that the test()
method is for checking string matches. I have checked and confirmed that my variable duplicate
is a string, but it seems to still be recognized as an object. An error in JavaScript is returned:
Uncaught TypeError: Object <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2c464344426c4843414d4542024f4341">[email protected]</a> has no method 'test'
What could be the reason for this occurrence?