Jump to content

Associative Arrays


killboy

Recommended Posts

Hi there.Is it possible to create associative arrays and fill them with data as one can do in PHP?For example, in PHP you can make:

$array=array("option1", "option2");

And when you wanna introduce some data you just:

$array["option1"][]="some string here";$array["option2"][]=5;

Can I do such a thing in JavaScript??

Link to comment
Share on other sites

No, but you can try JS Objects:myVar = new Object();myVar.option1 = "some string here";myVar.option2 = 5;
Yeah, but what if I do this:
$array["option1"][]="some string here";$array["option1"][]="another string";...$array["option1"][]=58748;$array["option1"][]=23;

As you see, PHP lets me introduce data dynamically into the associative array, whereas guided by the JS Object I'd have to do this:myVar.option1 = "some string here";myVar.option2 = "another string";myVar.option3 = 25;...myVar.optionn = 5;I'd have to have an option_i everytime I want to introduce some data.

Link to comment
Share on other sites

Yes, you can do it with Javascript. Javascript uses "soft" objects, essentially arrays and objects are the exact same structure. You will want to define your variable as an array though so you can use the array methods. The push method will add a new element to the end of the array. The pop method will remove an element from the end. The shortcut in PHP where you use empty brackets to add a new element is identical to using PHP's array_push function.

<html>  <head>  <script type="text/javascript">  myVar = new Array();    myVar.option1 = "some string here";  alert(myVar["option1"]);  myVar["option2"] = "another string";  alert(myVar.option2);  myVar.option3 = 25;  myVar.push("next val");  myVar.push("next val");  myVar.push("next val");  alert("myVar has " + myVar.length + " elements");  for (i in myVar)  {	alert(i + "=" + myVar[i]);  }  </script>  </head>  <body></body></html>

Note that myVar.length only returns the numeric elements added with the push method, it doesn't count the 3 other elements (technically they are properties, not array elements, even though you can reference them either way), even though all elements get enumerated in the for loop.myVar["option1"] = new Array;myVar["option1"].push(58748);

Link to comment
Share on other sites

Umm almost. var is a keyword in JavaScript, so you will have to call it something else, and some value will have to be an array for it to work. The push() method basically just adds another element to the end of an array.

var myArray = new Array();myArray['option1'] = new Array();myArray['option1'].push("Hello everyone");myArray['option1'].push("Goodbye");//Now myArray['option1'][0] == "Hello everyone" and myArray['option1'][1] == "Goodbye"

Link to comment
Share on other sites

Umm almost. var is a keyword in JavaScript, so you will have to call it something else...
Yes, I just noticed... :)Anyways, thanks a lot, that's what I was trying to achieve in JavaScript!Thanks for the help people!!
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...