dlofrodloh
dlofrodloh

Reputation: 1744

How to use an array of objects properly

I'm trying to assign the values of a split string in to a global array of objects. The string is called result and looks something like: "John.Doe.100.New-Mike.Jordan.200.Veteran-".

Splitting the string works fine, but I'm having trouble assigning the corresponding values into the object array. which doesn't work at all. Any idea where the problems are?

var UserData[]=new Object();

function SplitDatabase(result){
    var RawUsers = result.split('-');

    for (var i = 0; i < (RawUsers.length-1); i++) {
    var tempUserData=RawUsers[i].split('.');

    for (var x=0; x < (tempUserData.length);x++){

        switch (x)
            {
              case 0:
                     UserData[i].firstname=tempUserData[x];
                      break;
              case 1:
                      UserData[i].lastname=tempUserData[x];
                      break;
              case 2:
                      UserData[i].points=tempUserData[x];
                      break;
              case 3:
                      UserData[i].rank=tempUserData[x];
                      break;
              }
          }

      }
}

Upvotes: 1

Views: 86

Answers (3)

Philipp
Philipp

Reputation: 69673

The line

var UserData[]=new Object(); // <- wrong

is not the correct syntax to initialize an empty array in Javascript. To create an array, do this:

var UserData = [];

Arrays in Javascript are, just like everything else, by default untyped. They can store anything. To store an object in them, you first have to create one:

var UserDataEntry = {};

Then you can assign properties to this empty object:

UserDataEntry.firstname = tempUserData[0];
UserDataEntry.lastname = tempUserData[1];

And then you can put that new object into the array:

UserData.push(UserDataEntry);

The method push adds it as the last element of the array.

Alternatively, you can use the JSON syntax and initialize the object with the properties the moment you create it:

var UserDataEntry = {
     firstname:tempUserData[0],         
     lastname:tempUserData[1],
     // ...
};

An even more elegant way to solve this problem is to use the JSON-syntax to pass an unnamed object right to UserData.push:

UserData.push({
     firstname:tempUserData[0],         
     lastname:tempUserData[1],
     // ...
});

Upvotes: 2

Andy
Andy

Reputation: 63514

You need to create an object at UserData[i] otherwise you can't pin properties to it.

var UserData = [];

function SplitDatabase(result){
  var RawUsers = result.split('-');
  for (var i = 0, l = RawUsers.length; i < l; i++) {
    var tempUserData = RawUsers[i].split('.');
    UserData[i] = {};
    for (var x=0, xl = tempUserData.length; x < xl; x++) {
      switch (x) {
      case 0:
        UserData[i].firstname=tempUserData[x];
        break;
      case 1:
        UserData[i].lastname=tempUserData[x];
        break;
      case 2:
        UserData[i].points=tempUserData[x];
        break;
      case 3:
        UserData[i].rank=tempUserData[x];
        break;
      }
    }
  }
}

Fiddle

Upvotes: 1

Bucket
Bucket

Reputation: 7521

Issue 1 - var UserData[]=new Object(); is going to give you trouble. Use var UserData = []; instead. This is akin to Java syntax, not Javascript.

Issue 2 - for (var i = 0; i < (RawUsers.length-1); i++) { ... } will not iterate over the last element in RawUsers. change his to for (var i = 0; i < (RawUsers.length); i++) {.

Upvotes: 3

Related Questions