0

I'm so confused. The problem is that an object loses its values in a loop depending on how you access it. If I access via variables everything works fine. If I use static expressions (just the same as the variables) I don't have the right value. But let's look at the code:

THE FUNCTION ITSELF:

//Creating function with an input parameter (inputString)
function weirdBehaviour(inputString){

    //initializing the outputObject which will be returned
    var outputObject = {PROP1: "", PROP2: ""}

    // Loop through the return-Object
    for(x in outputObject){

        // Split input at <PROP1> etc. 
        var res = inputString.split("<" + x.toString() + ">");
        // Split "splitted input" again at </PROP1> etc.
        var res2 = res[1].split("</" + x.toString() + ">");

        // res2[0] is now the String between <PROP1></PROP1>
        outputObject.x = res2[0];

        // Log gives x and the value assigned to x
        console.log("-LOOP-START--------------------------------------");
        console.log("This works: ");
        // This works just fine
        console.log(x.toString() + ": " + outputObject.x);

        console.log("This doesn't: ");
        // Should be exactly the same but isn't
        console.log("PROP1: " + outputObject.PROP1);
        console.log("PROP2: " + outputObject.PROP2);
        console.log("-LOOP-END----------------------------------------");
    }
}

ACCESS EXAMPLE:

weirdBehaviour("<PROP1>String between prop1-tags</PROP1><PROP2>Prop2-Tag-String</PROP2>");

The output:

PROTOKOLL: -LOOP-START-------------------------------------- 
PROTOKOLL: This works:  
PROTOKOLL: PROP1: String between prop1-tags 
PROTOKOLL: This doesn't:  
PROTOKOLL: PROP1:  
PROTOKOLL: PROP2:  
PROTOKOLL: -LOOP-END---------------------------------------- 
PROTOKOLL: -LOOP-START-------------------------------------- 
PROTOKOLL: This works:  
PROTOKOLL: PROP2: Prop2-Tag-String 
PROTOKOLL: This doesn't:  
PROTOKOLL: PROP1:  
PROTOKOLL: PROP2:  
PROTOKOLL: -LOOP-END---------------------------------------- 

I'm so confused... So so strange. I hope someone can help.

Thanks and regards, OL

OddDev
  • 3,644
  • 5
  • 30
  • 53

2 Answers2

2

On this line:

outputObject.x = res2[0];

you're creating a property on the object called x, you're not creating a property using the name stored in x. To do that, use brackets notation:

outputObject[x] = res2[0];

...and similarly later when you read it:

console.log(x + ": " + outputObject[x]);

(You don't need to use x.toString(), x is already a string.)

In JavaScript, you can access properties using dot notation and a property name literal (obj.foo), or brackets notation and a property name string (obj["foo"]). In the brackets notation, the string can be the result of any expression, including a variable lookup. So these are all the same, for instance:

obj.foo
obj["foo"]
x = "foo"; obj[x]
obj["f" + "o" + "o"]
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

You should be using:

outputObject[ x ] = res2[0];

instead of

outputObject.x
fallenland
  • 525
  • 2
  • 6
  • Thanks :) T.J. Crowder's answer was a bit more detailed that's why he is getting the tick ;P – OddDev Oct 15 '14 at 08:55