Ian presented the following question to me: How do you initialize an associative array with default values in javascript? There is plenty of posts out there on the internet in regards to creating arrays, most look something like:
var myArray = new Array();
myArray["something"] = "some value";
and so on. But what he was looking for was something more like the php method:
$myArray = ( "something"=> "some value", "something else" => "another value");
after a small amount of scouring remembering something about colons, I finally found a scathing post by someone with the revelation: Javascript doesn't actually have associative arrays. In fact javascript doesn't have arrays at all. It has objects. So how do you initialize an associative array in javascript? You don't. You initialize an object:
var arrayTest = {"something":"1st Result","second":"2nd Result"};
alert(arrayTest["second"]);
Hopefully that helps someone out there in Google land.
Showing posts with label json. Show all posts
Showing posts with label json. Show all posts
Initializing an Associative Array in Javascript
Posted by
sarahbee
on Wednesday, August 26, 2009
Labels:
arrays,
associative array,
declaring values,
javascript,
json,
objects,
php
/
Comments: (1)
Learning Ajax, Json, with php and database manipulation.
Posted by
sarahbee
on Monday, July 20, 2009
I've been struggling today and last week to find some good tutorials or a well recommended book for getting the ball rolling in my quest to learn Ajax. I figure I might as well try to start off working with Json instead of wasting time with parsing string data. So far here are the links I've found useful:
Quick run down on JSON:
Understanding JSON: the 3 minute lesson
JSON on Wikipedia
Excellent Starter tutorial using Ajax (and ASP, but can be easily modified to a php date() function)
W3schools Ajax Example
More Advanced and In Depth Series on Ajax covering numerous topics, which I breezed through the first 3. A much longer read, but contains some really useful details:
Mastering Ajax: 11 Part Series from IBM developerWorks
After you do the W3 Schools Tutorial, I suggest moving on to this one, which uses Ajax, Json, Php, and Database manipulation. Heads up - I had to remove the $$link variable variables from the database functions in order for it to work.
Json Ajax Webchat Tutorial
Good luck, and if you know of some additional resources I would appreciate the links.
Quick run down on JSON:
Understanding JSON: the 3 minute lesson
JSON on Wikipedia
Excellent Starter tutorial using Ajax (and ASP, but can be easily modified to a php date() function)
W3schools Ajax Example
More Advanced and In Depth Series on Ajax covering numerous topics, which I breezed through the first 3. A much longer read, but contains some really useful details:
Mastering Ajax: 11 Part Series from IBM developerWorks
After you do the W3 Schools Tutorial, I suggest moving on to this one, which uses Ajax, Json, Php, and Database manipulation. Heads up - I had to remove the $$link variable variables from the database functions in order for it to work.
Json Ajax Webchat Tutorial
Good luck, and if you know of some additional resources I would appreciate the links.