Is there a way for this line to always work and not throw TypeError: Cannot read property 'Whatever' of undefined
var MyArray = [];
MyArray[StringVariableName][StringVariableName2].push("whatever");
Is there a way for this line to always work and not throw
|
||||
Try this:
|
|||||||||||||
|
To check without getting an error: this snippet allows you to check if a chained object exists.
from http://stackoverflow.com/a/21353032/2450730 Shorthand creation of object chains in Javascript this function allows you to create chained objects with a simple string.
usage
from http://stackoverflow.com/a/21384869/2450730 both work also for arrays with a simple change.replace if you have any questions just ask. |
||||
|
I think instead of using array in the first place, use object if your keys are not integers. In Javascript Arrays are also object So it is not wrong to do this
I think it is conceptually wrong So instead of using Array to hold such pair of data you should use objects. See this:
|
||||
|
You could use the literal syntax to set things up like you'd have them:
|
|||||
|
MyArray = MyArray || [];
– Cory Danielson Feb 18 '14 at 16:15