开发者

JavaScript Array / Struct

开发者 https://www.devze.com 2023-01-30 03:09 出处:网络
I would like to create a structure within javascript. I have a pair of info开发者_StackOverflowrmations, I would like to use, example:

I would like to create a structure within javascript. I have a pair of info开发者_StackOverflowrmations, 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!


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
}


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.


You can define arrays and generic objects in pure JavaScript/json:

var array = []; // empty array
array.push({name: 'parameter-name', value: 'parameter-value'});
array.push({name: 'parameter-name2', value: 'parameter-value2'});
console.log(array);
// Output: 
// [Object { name="parameter-name", value="parameter-value2"}, Object { name="parameter-name2", value="parameter-value2"}]

You can also define the same array like so:

var array = [
  {name: 'parameter-name', value: 'parameter-value'}, 
  {name: 'parameter-name2', value: 'parameter-value2'}
]; 

As far as looping through the array:

for (var i = 0; i<array.length; i++) {
  var elem = array[i];
  console.log(elem.name, elem.value);
}

// Outputs:
// parameter-name parameter-value2
// parameter-name2 parameter-value2


I'd store object literals in the array, like so:

var myArray = [];

myArray[0] = {name:"some name", value:"some value"};
myArray[1] = {name:"another name", value:"another value"};

for (i=0; i < myArray.length; i++) {
    console.log(myArray[i].name + ' / ' + myArray[i].value);
}


// initialize empty array    
var myArray = [];

// fill it with an object - the equivalent of a struct in javascript
myArray.push({
  name: 'example-name'
  value: 'example-value'
});
// repeat as neccessary

// walking through the array
for (var i = 0; i < myArray.length; i++)
{
  // retrieving the record
  record = myArray[i];

  // and accessing a field
  doSomething(record.name);
}


var array = {paramName: 'paramValue', paramName2: 'paramValue2'};

for(i in array) {
    _tag.i = array.i;
}


There is no "Struct" in JavaScript only Object

my_array = new Array();
my_array.push({name: 'john', age:31});
my_array.push({name: 'da_didi', age:120});

for (i=0; i<my_array.length; i++)
{
    alert(my_array[i].name);
}


How about

function Struct(name, value) {
  this.name = name;
  this.value = value;
}

arr[0] = new Struct("name1", "value1");


Javascript objects are loose objects: properties can be added and removed dynamically. So making a new Struct(), as you suggest, does -not- guarantee that the returned object will always have the properties you expect it to have. You have to check the availability of properties at the point of usage (duck typing):

var i, element;
for (i = 0; i < array.length; i++) {
  element = array[i];
  if (Object.hasOwnProperty.call(element, "name")
      &&  Object.hasOwnProperty.call(element, "value")) {
    _tag[element.name] = element.value;
  }
}

(Also, I'm just guessing that _tag is an object itself, but that wasn't clear from your example.)

You could probably use a more succinct syntax, but that depends heavily on the values of the properties. For example, you -might- be able to use something like this:

var i, element;
for (i = 0; i < array.length; i++) {
  element = array[i];
  if (element.name && element.value) {
    _tag[element.name] = element.value;
  }
}

But you need to realize that the above condition will be false not only if one or both of the properties (name and value) are undefined but also if the value of either or both refers to the empty string, null, 0, NaN, or false.

0

精彩评论

暂无评论...
验证码 换一张
取 消