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
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
Share
Improve this question
edited Nov 4, 2012 at 14:04
user743382
asked Nov 4, 2012 at 13:55
SablefosteSablefoste
4,1923 gold badges40 silver badges60 bronze badges
3
- why you want start your index from 5? – Anoop Commented Nov 4, 2012 at 13:59
- 3 Javascript does not support associative arrays. You can use, however, an object. Albeit the syntax is different. – Tivie Commented Nov 4, 2012 at 14:01
- To whoever had reviewed the suggested edit: please don't approve it as is, if it overwrites changes from a later edit. – user743382 Commented Nov 4, 2012 at 14:05
6 Answers
Reset to default 11What you want is an object:
var d_names = {
5: "Sunday",
6: "Monday"
//...
};
You can then get "Sunday"
like this:
var sunday = d_names[5];
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"