Javascript variable definition

Good evening,

I’m trying to figure a way of defining a variable in JScript so I can adjust more easely a translation from VB.Net I must do.

The var is of the type Variable1[index].property, that is: it goes from index 0 to n, and each one has their set of values like:

Variable1[0].Name = "One"
Variable[0].Value = 12
Variable1[0].VarType = "something"

Variable1[1].Name = "Two"
Variable[1].Value = 1.222
Variable1[1].VarType = "whatever"

etc.

If I create a var like this:

var obj = {
Name: "dummy",
Value: 0,
VarType: "dummy"
}

how can I create an array with these fields (or more) in each array index?

Kind regards

I’m guessing you’re looking to create an array of objects, in which case, it should look something like this:

var arr = [{
    Name: "one",
    Value: 12,
    VarType: "something"
},
{
    Name: "two",
    Value: 1.222,
    VarType: "other thing"
}];

Is that what you had in mind?

Hi

Yes, that’s it. I’ve only found a solution. I created an object, like this:

var obj = {
    Name: "",
    Value: 0,
    VarType: ""
};

And then populated with 30 empty elements (I don’t need more):

var Variable1 = [];
for(i=0;i<=30;i++){
Variable1[i] = new Array (obj);
}

This way I can attribute a value to the nth element, like Variable1[n].Name = "whatever".

I Think I can add more elements - if needed - by Variable1.push(obj)

Cheers

Hi,

This won’t work as you maybe think it does. As objects are passed by reference in JavaScript you are creating an array of arrays, where the first element of each is the same object.

Here’s an example (updated to use ES6 syntax):

const obj = {
  name: '',
  value: 0,
  varType: '',
};

const arr = [];

for (let i = 0; i <= 5; i += 1) {
  arr[i] = new Array(obj);
}

console.log(arr);

obj.name = 'Pullo';

Inspect the output in the console and you will see that the name property of every single object has been set to “Pullo”.

If you then do arr[0].name = "ruikepler" you are just setting a property on the array itself (arrays are also objects). The original object remains untouched.

[
  [
    { name: "Pullo", value: 0, varType: ""}, 
    name: "ruikepler"
  ],
  [
    { name: "Pullo", value: 0, varType: ""}, 
  ]
  ...
]

If this is what you intended, apologies. Forget I spoke…

Otherwise, you might be wanting to do something like this:

const arr = [];

for (let i = 0; i <= 5; i += 1) {
  arr[i] = {
    name: '',
    value: 0,
    varType: '',
  };
}

arr[0].name = 'Pullo';
arr[1].name = 'ruikepler';

console.log(arr);

This gives you:

[
  {name: "Pullo", value: 0, varType: ""},
  {name: "ruikepler", value: 0, varType: ""},
  {name: "", value: 0, varType: ""},
  ...
]
1 Like

Hi

It makes sense. Still, the method I presented is giving diferent values: not equal ones.

I’m conducting tests in Chrome and Firefox

Kind regards,

That’s because with this line

Variable1[n].Name = "whatever"

you’re assigning "whatever" to the name property of the array, not the contained object as you probably intended – as @James_Hibbard pointed out, you’re creating an array of arrays here, each of which contains a reference to obj as their first and only elements. Such an assigment is possible because arrays are also objects in JS; you could represent the result roughly like

{
    0: {
        0: obj,
        length: 1,
        name: 'whatever'
    },
    1: {
        0: obj,
        length: 1
    },
    2: {
        0: obj,
        length: 1
    },
    // etc...
    29: {
        0: obj,
        length: 1
    },
    length: 30
}

I Think I can add more elements - if needed - by Variable1.push(obj)

In this case however you’d be pushing the object directly (without the wrapping array), and modifying one would indeed affect all other references; so you’ll have to push an object literal instead.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.