1

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;
              }
          }

      }
}
7
  • 2
    First: what on earth is var UserData[]? Commented Sep 27, 2013 at 14:23
  • 1
    @Qantas94Heavy -- Looks like some Java syntax creeping over. Commented Sep 27, 2013 at 14:23
  • 1
    Please describe what you mean with "doesn't work at all". What excatly do you expect to happen and what happens instead? Commented Sep 27, 2013 at 14:23
  • this code does not work - have you checked your error console? you can't have a syntax like this in JavaScript: var UserData[]=new Object(); Commented Sep 27, 2013 at 14:24
  • I'm not sure about object arrays. The script doesn't even load properly with the obviously broken object code in. Commented Sep 27, 2013 at 14:25

3 Answers 3

3

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++) {.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, I'll try that. By the way, issue 2 is fine because the last part of the string is "-" which returns an empty user. Just the way my AJAX is.
Ok, I've tried the solution for Issue 1, and UserData[i].firstname=tempUserData[x]; does not run. I've put an alert after that and the alert doesn't get shown.
What is your console output? This would give us a better idea of what's going on.
2

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],
     // ...
});

3 Comments

Ok almost working, I put UserData.push(UserDataEntry); at the end of the case3 in the switch. But the push is overwriting previous objects in the array, which doesn't make sense to me.
The first alert shows the correct data as it loops, then the second alert just shows the last record over and over again.
I'll start a new question, because it's a different issue. Thanks for your help!
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.