0

I have a little problem. From webmethod returned two dimensional array.

My webmethod:

[WebMethod]
public static string[,] Test()
{
string[,] arry1 = new string[2, 2];
arry1[0, 0] = "test00";
arry1[0, 1] = "test01";
arry1[1, 0] = "test10";
arry1[1, 1] = "test11";

return arry1; 
}

in my js code im using this way....

var arry1=new Array();
$.ajax({
url: "test.aspx/Test",
data: {},
cache: false,
async:false,
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "GET",
success: function (data) {
    arry1 = data.d;



},
error: function (response) {
    alert(response);
}
});
alert(arry1[1,1]); //test01  why not test11?

How can i do this?

Edit.. Array

"test00" "test01";
"test10" "test11";

in asp.net

arry1[0, 0] = "test00";
arry1[0, 1] = "test01";
arry1[1, 0] = "test10";
arry1[1, 1] = "test11";

in javascript

arry1[0] // test00
arry1[1] // test01
arry1[2] // test10
arry1[3] // test11

2 Answers 2

1

Based on your array running alert(arry1[1,1]); would just return the value at position 1 which in the case of JavaScript, is an array, so your result will be ,test01 if you wanted to get test11, you would have to do alert(arry1[3][1]);, which would get the fourth position in the array [3], and then the value at [1] of that array.

//What your array might like in JS
var arry1 = [[null,"test00"], [null,"test01"], [null,"test10"], [null,"test11"]];
alert(arry1[1,1]); // test01
alert(arry1[3][1]); // test11

Fiddle

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

1 Comment

@user3551859: No probs, sorry I only just noticed my fiddle link was messed up, should be okay now if you still want to play around.
0

Javascript, unlike other languages, does have 2D arrays. Rather, it has arrays of arrays.

That means you need a slightly different syntax

alert(arry1[1][1]);

2 Comments

hi. arry1[1][1] alerted only 'e'.-->> t'e'st11
Then you need to console.log arry1 and make sure you have what you think you have.

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.