Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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");
share|improve this question
    
MyArray = MyArray || []; –  Cory Danielson Feb 18 '14 at 16:15

4 Answers 4

up vote 5 down vote accepted

Try this:

var MyArray = [];
MyArray[StringVariableName] = MyArray[StringVariableName] || [];
MyArray[StringVariableName][StringVariableName2] = MyArray[StringVariableName][StringVariableName2] || [];
MyArray[StringVariableName][StringVariableName2].push("whatever");
share|improve this answer
    
Works amazing, can you explain what that assignment means? –  Nadav Miller Feb 18 '14 at 16:30
    
@NadavMiller It simply is a logical OR. If first part is true, second part is not executed and first part is assigned to the variable. –  blunderboy Feb 18 '14 at 16:31
    
It is the logical OR operator and it simply says: If the left operand is a truthy value then return it otherwise return the right operand. –  ppoliani Feb 18 '14 at 16:33

To check without getting an error:

this snippet allows you to check if a chained object exists.

var x;
try{x=MyArray[name1][name2][name3][name4]}catch(e){}
!x||(x.push('whatever'));

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.

function def(a,b,c,d){
 c=b.split('.');
 d=c.shift();//add *1 for arrays
 a[d]||(a[d]={});//[] for arrays
 !(c.length>0)||def(a[d],c.join('.'));
}

usage

var MyArray={};//[]
def(MyArray,'name1.name2.name3.name4');//name1+'.'+name2....

from

http://stackoverflow.com/a/21384869/2450730

both work also for arrays with a simple change.replace {} with []

if you have any questions just ask.

share|improve this answer

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

var a = [];
a['key'] = 'something';

console.log(a); //Gives []

I think it is conceptually wrong So instead of using Array to hold such pair of data you should use objects. See this:

var myObject = myObject || {};
myObject[str1] = myObject[str1] || {};
myObject[str1][str2] = myObject[str][str2] || [];

// Now myObject[str1][str2] is an array. Do your original operation

myObject[str1][str2].push("whatever");
share|improve this answer

You could use the literal syntax to set things up like you'd have them:

var myObj = {
    StringVariableName: {
        StringVariableName2: []
    }
};

myObj.StringVariableName.StringVariableName2.push("whatever");
share|improve this answer
    
You are doing it wrong way because StringVariableName looks like actually a variable name. You are making them as constant keys in the object. –  blunderboy Feb 18 '14 at 16:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.