0

I have a multidimensional array in which some values are present i want to retrieve the [0][1] or [1][1] index value. I am getting undefined as array, if i tried to directly try to get the array value am able to get the value.

This what i want to achieve

I had a select drop down menu , According to the selected index i need to retrieve a message from the array box. For say if the index is 1 then i had to get [1][1] array index value if it is zero then [0][1] array index value

This is the fiddle what i have done. http://jsfiddle.net/hTQZ9/

3
  • why do you create an array inside an array? just add a value to the index! Commented May 24, 2012 at 11:58
  • @Bergi am not able to retrieve the value from the array Commented May 24, 2012 at 12:03
  • @p0rter this mutlidimensional array is also being used at other places , that why it being created like this. to match the key with the values Commented May 24, 2012 at 12:04

3 Answers 3

1

see this update: http://jsfiddle.net/hTQZ9/1/

var MessagesObj = { testName: [] } MessagesObj["testName"].push(["testName_custom_message_Label1val","Custom message for label 1"]); MessagesObj["testName"].push(["testName_custom_message_Label2val","Custom message for label 2"]); alert(MessagesObj["testName"][1][1]); var classChk = $(".customCheckEnabled").get(0); var getClassindex = classChk.selectedIndex; var getVarName = classChk.id var getCstMsgName = MessagesObj[getVarName]; alert(getCstMsgName); var getMessage = getCstMsgName[getClassindex][1]; alert(getMessage); 
Sign up to request clarification or add additional context in comments.

1 Comment

The array is already present in the database i cannot modify that
0

getCstMsgName is a string, not an array.

One way is to use this

$(document).ready(function () { var testName_MessagesArray = new Array(); var cntr = 0; testName_MessagesArray[cntr] = new Array("testName_custom_message_Label1val", "Custom message for label 1"); cntr++; testName_MessagesArray[cntr] = new Array("testName_custom_message_Label2val", "Custom message for label 2"); cntr++; alert(testName_MessagesArray[1][1]); var classChk = $(".customCheckEnabled"); alert(classChk); this.testName = testName_MessagesArray; //<-- set this with the name var getClassindex = classChk.attr("selectedIndex"); alert(getClassindex); var getVarName = classChk.attr("id"); alert(getVarName); var getCstMsgName = this[getVarName]; //<-- reference it from this alert(getCstMsgName); var getMessage = getCstMsgName[getClassindex][1]; alert(getMessage); }); 

​ If testName_MessagesArray is in global scope, you can do window["testName_MessagesArray"] to reference it. Your current example is local scope so that would not work.

Comments

0

You should really use an array literal instead of, er, cntrs:

var testName_MessagesArray = [ ["testName_custom_message_Label1val", "Custom message for label 1"], ["testName_custom_message_Label2val", "Custom message for label 2"] ]; 

Then, if you want to retrieve a value from it, use testName_MessagesArray[x][y].

What you were doing:

var classChk=$(".customCheckEnabled"); // this is a jQuery element, alerted as [object Object] var getClassindex=classChk.attr("selectedIndex"); // this is 0 or 1 var getVarName=classChk.attr("id"); // this will be the id of the first selected element, a string var getCstMsgName=getVarName+"_MessagesArray".toString(); // this will create a string, from the "getVarName"-string and your string-literal-toString var getMessage=getCstMsgName[getClassindex][1]; // as "getCstMsgName" is a string - not the twodimensional array, // getCstMsgName[getClassindex] returns the character at the selected index (as a string) // and getCstMsgName[getClassindex][1] results in the second character of the one-character-string - undefined 

1 Comment

Can you create a fiddle for an example

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.