Question:
JavaScript: new Array()?
anonymous
2009-08-26 01:24:23 UTC
is there an easier way to define a multi-dimensional array?
****
notify = new Array(5);
notify['code'] = new Array();
notify['type'] = new Array();
notify['reason'] = new Array();
notify['solution'] = new Array();
notify['timeout'] = new Array();

________
And-
Can you set 5, and 7 without (1,2,3,4, 6) being set?
notify['code'][5] = 'qwerty';
notify['code'][7] = 'asdfgh';
Four answers:
§©®Î¶†Δ®
2009-08-26 09:41:11 UTC
There are other ways to define your data. You can use both array-literal notation and object-literal notation. You can even use a combination of both or even other data types to create more complex data structures.



In your first section, the "notify array" is really being used like an object with array notation (the square brackets as subscripts). When you use object-literal notation, you replace the square brackets with curly braces and use named values instead of indices. This is also called a hashtable. In this example, I'm creating an object with arrays for properties:



var notify1 = {

code: [],

type: [],

reason: [],

solution: [],

timeout: []

};



You can access the properties of the new "notify object" like this (for example):

notify1.code[0]; // object notation

or

notify1["code"][0]; // array notation



I could also do this to create an array of objects:



var notify2 = [

{code: "", type: "", reason: "", solution: "", timeout: 0},

{code: "", type: "", reason: "", solution: "", timeout: 0},

{code: "", type: "", reason: "", solution: "", timeout: 0}

];



You could access this array of objects like this:

notify2[0].code

or

notify2[0]["code"]



... it's up to you. For more information, take a look at JSON (JavaScript Object Notation): http://www.json.org/



Regarding your other question, JavaScript arrays are usually sparse (unless defined using the new Array() syntax... depending on the browser). Any locations in the array that are not initialized are set to undefined. So you can do this:

var test1 = {

code: []

};

test1.code[5] = "qwerty";

test1.code[7] = "asdfgh";

alert( test1.code[5] ); //displays "qwerty"

alert( test1.code[2] ); //displays "undefined"



or



var test2 = {

code: [,,,,,"qwerty",,"asdfgh"]

};

alert( test2.code[5] ); //displays "qwerty"

alert( test2.code[2] ); //displays "undefined"
?
2016-11-30 09:30:06 UTC
i don't comprehend why you're making use of a loop. in case you're only desirous to keep the $_POST['names'] array interior the $names variable in simple terms use: $names = $_POST['names']; the clarification it would desire to no longer be working is the form you're imparting your javascript array, it is going to be interior the format: names[key1] = value1 names[key2] = value2
santhosh
2009-08-26 02:25:22 UTC
Javascript Array.. I feel that this is the simple way to declare..

words = new Array("li","l","fh","comete","In","t")
'Æ тσичρкεя13 - Tech Guru
2009-08-26 01:30:35 UTC
I'm pretty sure that's the most simple version, there are other ways but not as easy.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...