0

I have a post function that returns a 2d array. How would I go about displaying each of the elements in it? My code looks something like this :

$.post("/Question/GetPollQuestionsForView/", { poll_ID: pollId }, function(result) {
   //$("#CustomerList tbody").append($(result));
   var myarray = new Array()
   myarray = result;
   alert(myarray);
});

what the alert returns is "System.String[][]". How can i go about appending each of the values from my array to my div tag called #divComparativeQuestions.

1
  • If the alert shows "System.String[][]", the myarray is not an array but a string. You have to send the correct data from the server. Btw. you could just do: var myarray = result;. Commented May 29, 2011 at 9:40

2 Answers 2

1

For example:

var data = new Array();
for(var i=0;i<myarray.length;i++){
   data.push(myarray[i].join(', '));
}
$('#divComparativeQuestions').html(data.join('<br/>'));

(hope this works, not tested :), but you get the idea )

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

1 Comment

throws an error, on data.push(myarray[i].join(', ')); " Microsoft JScript runtime error: Object doesn't support this property or method "
0

I'm guessing you want something like:

// given an array [[1,2],[3,4]] with a desired result of <div>1234</div>
$.post("/Question/GetPollQuestionsForView/", { poll_ID: pollId }, function(data) {
    if(data) {
        var div = $('#divComparativeQuestions');
        $.each(data, function(index, element) {
            div.append(element); // will be inner array of [1,2] or [3,4]
        }); 
    }
});

All pretty basic stuff, in this case I'm taking advantage of the fact js typically flattens array as strings without commas to get the desired out put, but if you want to delimit them items somehow or wrap them in tags or whatever, it's easy enough to work out by taking a few seconds to browse http://docs.jquery.com

2 Comments

"Doesn't work" doesn't really help me fix that. How so? Syntax error? Doesn't do anything? What is the data your post to that url is meant to return?
Never mind, I didn't notice your alert is actually returning a string rather than you just describing the type. Yeah, you should change your code to return some valid JSON data or something similar ideally. The above code should operate correctly on a javascript array.

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.