Question

In Javascript, know I can set an array so that the key is a autonumbered (starting at 0) assigned array:

var d_names = new Array("Sunday", "Monday", "Tuesday", 
              "Wednesday", "Thursday", "Friday", "Saturday");

// Key for Sunday is '0' 

and if I want to assign keys, I could do:

    var d_names={};
    d_names[5]="Sunday";
    d_names[6]="Monday";
    d_names[7]="Tuesday";
    d_names[8]="Wednesday";
    d_names[9]="Thursday";
    d_names[10]="Friday";
    d_names[11]="Saturday";

    // Key for Sunday is '5'

But is there a shorthand way to assign the keys like in PHP?

var d_names = new Array(5=>"Sunday", 6=>"Monday", 7=>"Tuesday", 8=>"Wednesday", 
            9=>"Thursday", 10=>"Friday", 11=>"Saturday"); 

// Doesn't work
Was it helpful?

Solution

What you want is an object:

var d_names = {
    5: "Sunday",
    6: "Monday"
    //...
};

You can then get "Sunday" like this:

var sunday = d_names[5];

OTHER TIPS

In PHP, an array with manually defined keys (as opposed to consecutive integers beginning with 0) is called an "associative array" - this is what you have in your example above with '=>' delimiting keys and values.

In Javascript, an "associative array" is technically an "object" (though everything in JS is an object - that's a more detailed topic though).

Shorthand for an "indexed array" (consecutive integer keys) in JS is:

var d_names = [
  'Sunday',
  'Monday',
  // etc.
];

whereas shorthand for an object (like an associative array) in JS is:

var d_names = {
  5: 'Sunday',
  6: 'Monday',
  // etc.
};

You should however be careful when using indexed arrays -vs- objects/associative in Javascript. Javascript is not PHP, and the fact that "everything is an object" has repercussions when looping. A notable difference is that for(var i=0; i<arr.length; ++i){} iterates over an arrays keys but for(var x in obj) {} iterates over an objects "members" which can differ depending on environment/browser/etc.

There are no associative arrays in JavaScript. You have two choices:

A simple object, used as a (unsorted!) key-value map - easy to write as a literal, also JSON syntax:

{
    "5": "Sunday",
    "6": "Monday",
    "7": "Tuesday",
    "8": "Wednesday",
    "9": "Thursday",
    "10": "Friday",
    "11": "Saturday"    
}

Or you can use a sparse Array, i.e. with no values for the properties 0 to 4. You can easily create it via

var arr = new Array(5); // creates an empty array object with length 5
arr.push("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

which is equivalent to the literal

[,,,,, "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

The following in php:

echo json_encode(new Array(5=>"Sunday", 6=>"Monday", 7=>"Tuesday",...11=>"Saturday")); 

Will produce JSON that looks like

   {"5":"Sunday","6":"Monday" , "7":"Tuesday"..."11":"Saturday"}

If sent via ajax and result is json parsed it will return an object:

 obj= { "5": "Sunday", "6": "Monday",....  "11": "Saturday"}

Now you can access using javascript object notation:

 alert( obj['5'] )/* SUnday*/

Not exactly but you can use an Object

var obj = {

5:"Sunday",
6:"Monday",
7:"Tuesday",
8:"Wednesday",
9:"Thursday",
10:"Friday",
11:"Saturday"    
}

If want to use array

var arr = [];
arr[4] = undefined;
arr.push("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

You can achieve that with this and some other tricks

 var d_names = [,,,,,"Sunday", "Monday", "Tuesday", 
          "Wednesday", "Thursday", "Friday", "Saturday"];

 alert(d_names[5]); //prints "Sunday"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top