Skip to content Skip to sidebar Skip to footer

Jquery Multitype Jagged Array Behaving Strangely

Please refer to this: http://jsfiddle.net/MasterOfKitties/v7xbu/7/ /*This is the desired behavior*/ /*var A = [1, 2, 3]*/ /*var B = 'hello', [1, 2, 3], 'hello', [2, 3, 2],

Solution 1:

you have to initialize strTemp with a value.

like

var strTemp = "";

instead of

var strTemp;

in your case.

when the line

 strTemp += "<P>" + B[x] + "</p>"

is executing for the first time strTemp is undefined so its converted to string as undefined and gets appended in the begining

see the working fiddle

Solution 2:

Your function does not "replicate"/copy the A array, it just pushes a reference to A into B. I believe you want:

B = A.slice(0); 

Now, you seem to have another problem: if you enter e.g. 10 and 11 into the prompts, you get this array at the end:

[
    "hello",
    [
        10,
        11
    ]
]

I suspect that's not really what you're looking for. Could you please explain what you're really trying to accomplish?

Post a Comment for "Jquery Multitype Jagged Array Behaving Strangely"