Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Array / Struct

Tags:

I would like to create a structure within javascript. I have a pair of informations, I would like to use, example:

array[1] = new Struct(); array[1].name = "parameter-name"; array[1].value = "parameter-value";  array[2] = new Struct(); array[2].name = "parameter-name2"; array[2].value = "parameter-value2"; 

This can be on a diffrent page with diffrent values, maybe on element within my array, maybe 2-20..

Later, within my generic javascript, I would like to parse the array and continue with my parameters, example:

for(i=1 to length_of_my_array) {   _tag.array[i].name = array[i].value;  } 

How can I realize this with pure javascript? Thanks for any hint!

like image 217
da_didi Avatar asked Dec 13 '10 17:12

da_didi


2 Answers

As long as you don't want any fancy features, it's really easy to create such structures in JavaScript. In fact, the code you posted will almost work, if you replace the new Struct() with this:

array[1] = {}; 

This creates an empty object, and you can put any properties you want in it, such as name and value.

To create an array, you can do something like this:

var array = []; // empty array  // object literal notation to create your structures array.push({ name: 'abc', value: 'def' }); array.push({ name: 'ghi', value: 'jkl' }); ... 

And to iterate over the array:

for (var i = 0; i < array.length; i++) {   // use array[i] here } 
like image 197
casablanca Avatar answered Oct 07 '22 21:10

casablanca


It would be good to find out more regarding the problem you are attempting to resolve.

I don't think there is an object in JavaScript called Struct, unless you define one.

I think what you are looking for is a JavaScript object instead of Struct. There are a number of ways to create a new object, and they can be nested in an array or in other objects.

myArray[0] = new Object(); myArray[0].name = "parameter-name"; myArray[0].value = "parameter-value";  myArray[1] = new Object(); myArray[1].name = "parameter-name2"; myArray[1].value = "parameter-value2"; 

Notice that I have changed your code in a couple of ways: 1. "array" is named "myArray" to clarify that we are referring to a particular array. 2. The first instance of myArray is 0. Arrays start at 0 in Javascript. 3. Struct is changed to Object.

myarray = [     {         "name":"parameter-name",         "value":"parameter-value"     },     {         "name":"parameter-name2",         "value":"parameter-value2"     } ]; 

This is an alternative syntax for doing the same thing. It uses "literal notation" to designate an array (the square brackets), and the objects (the curly brackets).

for(var i = 0; i < myArray.length; i++) {     for(key in myArray[i]) {         alert(key + " :: " myArray[i][key]);     } } 

This will loop over the array and alert you for each property of the object.

alert(myArray[0]['value']) //parameter-value myArray[0]['value'] = "bar"; alert(myArray[0]['value']) //bar 

Each property of each object can also be assigned a new value.

like image 43
Adolph Trudeau Avatar answered Oct 07 '22 21:10

Adolph Trudeau