lilHar
lilHar

Reputation: 1866

Accessing a multidimensional array using a one dimensional array in Javascript

If I have the following initialized... (fyi, in the code I'm basing it off of, both variables are dynamically created)

// Start of Code that MUST stay unchanged.
var myPrimaryArray = [["Potato","Walrus"],42,[["Your father was a hamster", "Target"],362]];
var locationArray = [2][0][1];
// End of Code that MUST stay unchanged

//What to put here to get "Target"?

How do I make this return "Target", with only those two variables? (Not knowing the depth into either array I have to go ahead of time.)

Upvotes: 0

Views: 354

Answers (2)

lilHar
lilHar

Reputation: 1866

Got a solution (altered text for stackoverflow, hope it still works for anyone seeking a solution similar to mine.) It's cumbersome, but it should work.

// Start of Code that MUST stay unchanged.
var myPrimaryArray = [["Potato","Walrus"],42,[["Your father was a hamster", "Target"],362]];
var locationArray = [2][0][1];
// End of Code that MUST stay unchanged


getArrayObjectWithLocationArray(myPrimaryArray,locationArray)



function getArrayObjectWithLocationArray(myArray, myLocationArray){
var myArray = stringToPositionArray(myString);
if (myLocationArray.length > 1}{
    return getPanelWithPositionString([myArray[myLocationArray[0]], myLocationArray.splice(0,1))
} else {
    return myArray[stringToPositionArray[0]];
}

}

Upvotes: 0

internals-in
internals-in

Reputation: 5038

Hopes this will return target

var myPrimaryArray = [["Potato","Walrus"],42,[["Your father was a hamster", "Target"],362]];
var locationArray = myPrimaryArray[2][0][1];
alert(locationArray);

output:

Target

To be fun , check your Code:

locationArray = myPrimaryArray [2][0][1];

Upvotes: 3

Related Questions