0

I have an array which I have another to push into in a loop

array1[ [box1, type1, 10], [box1, type2, 12] ]
array2[ [box2, type1, 10], [box2, type2, 12], [box2, type2, 12] ]

var curArray = new Array();

so in a loop:

var testarray = new Array('box1', 'type1', 10);
curArray.push(testarray);

and I have to post it so I set it in a hidden field. array.push works when I console.log(curArray) but if you set it to a hidden field by

$('hdnField').val(curArray)

it no longer become a multidimensional array.

Is there another way to do this?

3
  • 2
    is this an ajax post? if so can add data within ajax code and not need hidden field Commented Jul 21, 2015 at 14:33
  • hi @charlietfl, thanks for the reply. nope it's a form post Commented Jul 21, 2015 at 14:35
  • then answer below to parse to json is best way, decode the json at server if need to work with array there Commented Jul 21, 2015 at 14:38

2 Answers 2

3

try this:

$('hdnField').val(JSON.stringify(curArray));
Sign up to request clarification or add additional context in comments.

2 Comments

hi there, it turns it into a string, then I will decode it in the server?
YES, you can find easily a JSON Parser library in your server language
1

You can try JSON.stringify()

$('hdnField').val(JSON.stringify(curArray));

Does this work?

2 Comments

unfortunately, it did not work. having a weird error "Unexpected token b". i'm sure they wasn't any extra syntax that i mistyped.
it's not parse but stringify. like $('#hdnField').val(JSON.stringify(curArray));

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.