By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,520 Members | 1,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,520 IT Pros & Developers. It's quick & easy.

Associative arrays string comparison

P: 1
Here is my code using associative array -

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. var scripts = new Array();
  3. scripts[0] = "PHP , ASP";
  4. scripts[1] = "ASP , JAVA";
  5. scripts[2] = "JavaScript";
  6. scripts[3] = "HTML";
  7.  
  8. var kaka = scripts[0].toString();
  9. var group = "PHP";
  10.  
  11. var group = "php";
  12. if (group in kaka) document.write("match found");
  13. </script> 
I get error object expected in IE. I have tried using various comparison methods right from substring / == etc.

What am I missing?
Aug 8 '09 #1
Share this Question
Share on Google+
1 Reply


gits
Expert Mod 5K+
P: 5,390
currently you don't use an assoc array ... since JavaScript doesn't have such :) ...

i guess you want to find the matches in your constructed array that currently looks like:

Expand|Select|Wrap|Line Numbers
  1. ["PHP , ASP", "ASP , JAVA", "JavaScript", "HTML"]
so you might do that with a loop (assuming that we use your array):

Expand|Select|Wrap|Line Numbers
  1. var group = 'asp';
  2.  
  3. for (var i = 0, l = scripts.length; i < l; ++i) {
  4.     if ((new RegExp(group, 'gi')).test(scripts[i])) {
  5.         alert('match in ele: ' + i);
  6.     }
  7. }
kind regards
Aug 9 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.