Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 31-07-2009
Member
 
Join Date: Apr 2008
Posts: 19
To find key in array for Javascript.

Hello,

I want to know that how can I make an associative array in javascript? & How to find its length.
Can anyone please help me how to get javascript array key?

Thanks in advance.
Reply With Quote
  #2  
Old 31-07-2009
Member
 
Join Date: May 2008
Posts: 41
Re: To find key in array for Javascript.

Check if key exists or not in a JavaScript array

There is no easy in-built function in JS to check if a given key exists or not in a JavaScript array object.
The below small snippet works just fine:

Code:
<script type="text/javascript">
//lets setup a sample array and its key-and-values
var array=new Array();
array['name']='Rohan Shenoy';
array['gender']='Male';
array['age']='21';
 
//lets try to find an key which exists
if(array['name']==undefined)
{alert('That key is undefined');}
else
{alert('That keys exists');}
 
//lets try to find an key which does NOT exist
if(array['residence']==undefined)
{alert('That key is undefined');}
else
{alert('That keys exists');}
</script>
Reply With Quote
  #3  
Old 31-07-2009
Member
 
Join Date: Apr 2008
Posts: 22
Re: To find key in array for Javascript.

JavaScript Array get key

Code:
<html>
    <head>
        <h1>JavaScript Array get key</h1>    
        <title>JavaScript Array get key</title>
    </head>
    <body>
        <script language="javascript" type="text/javascript">
            array = {"W" : "WE", "L" : "LOVE","I":"INDIA" };
            for (key in array)
                document.write("For the Element " + "<b>"+array[key]+"</b>" 
                + " Key value is  " +"<b>"+key+"</b>"+"<br>");            
        </script>
        
    </body>
</html>
Reply With Quote
  #4  
Old 31-07-2009
Member
 
Join Date: Apr 2008
Posts: 19
Re: To find key in array for Javascript.

Thanks for the code snippet & such a prompt reply.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "To find key in array for Javascript."
Thread Thread Starter Forum Replies Last Post
Unable to find elements of a given type in an array in C# Kasper Software Development 4 09-02-2010 06:46 PM
How an Array Class is Used in Javascript? PsYcHo 1 Software Development 5 05-02-2010 12:04 AM
JavaScript to convert an Array to String KAMANA Software Development 3 09-12-2009 05:30 AM
How to find second largest value from array Jaheel Software Development 3 18-11-2009 11:42 AM
How to find a mode in an Array set Quattro Software Development 3 03-08-2009 11:55 AM


All times are GMT +5.5. The time now is 12:17 AM.