0

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.)

2
  • i have updated my answer , check it out! Commented Mar 31, 2013 at 1:26
  • I appreciate the updated answer, but you're still changing one of the initial vars, sorry if I'm unclear, ... the point is those are the initial vars I have to start out with. Both those vars will be dynamically generated, which is why I can't change them. As such, this assumes I don't actually know what the value in location array will actually be, or the actual layout of myPrimaryArray. All I know is that location array holds the position of where in myPrimaryArray "Target" is. If I could do something like "MyNewVar = myPrimaryArray[locationArray] that'd be great, but I don't think it works. Commented Apr 1, 2013 at 18:36

2 Answers 2

3

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];

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

3 Comments

I appreciate the suggestion, but the code I'm providing is what I have to work with (it's an example of something I can't really change). The point is that I have to get "Target" from myPrimaryArray using locationArray as its provided. Changing the locationArray isn't an option for me.
@RobG ha ha !, Sorry , for my bad english!
That's cool, my English is pretty rubbish at times too, and it's my first language!
0

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]];
}

}

Comments

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.