0

I've built this variable called DLP. When I'm done doing what i need to do with the page, the variable looks something like this:

[['0','A'],['1','B'],['2','C'] ]

How can I turn this variable into a multi-dimension array so that when i go:

DLP[1][1]

it returns my result?

Damien
  • 4,093
  • 9
  • 39
  • 52
  • 2
    `DLP[1][1]` *will* return a result, **B**. What is *your* result that you're expecting it to return? – Tom Jul 31 '15 at 21:12
  • I had a similar question to this once. My question and answers may be helpful: http://stackoverflow.com/questions/29738068/need-help-building-complex-js-object – Digital Brent Jul 31 '15 at 21:12
  • Do you mean that DLP is a string variable with the content as shown? Please clarify. –  Aug 01 '15 at 00:30
  • Exactly! DLP is a string variable that's put together by doing stuff on the page. – Damien Aug 01 '15 at 00:43

3 Answers3

1

There are two ways to start an array.

var array = new Array(); or var array = [1, 2, 3]

It is considered bad practice to do it the first way. You can do what you want like this.

var DLP = [['0','A'],['1','B'],['2','C'] ]

DLP[1][1] will return your result.

noahdotgansallo
  • 763
  • 1
  • 6
  • 15
1

// the data
var dlp = "[['0','A'],['1','B'],['2','C'] ]";

// make it JSON conform
// replace single quotation marks with double quotation marks
dlp = dlp.replace(/'/g, '"');

// parse the JSON string
// get an array
var dlpArray = JSON.parse(dlp);

// use the array
alert(dlpArray[1][1]);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

[['0','A'],['1','B'],['2','C'] ] is already a multi-dimensional array. DLP[1][1] will return 'B' as Tom said in the comments. If you want to get one of the complete arrays (example: if you wanted the array: "2,C" you would only need to reference DLP[3]). See this question for more information on how to work with multidimensional arrays: JavaScript multidimensional array

Community
  • 1
  • 1
Digital Brent
  • 1,275
  • 7
  • 19
  • 47