14

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");
1
  • 1
    MyArray = MyArray || []; Commented Feb 18, 2014 at 16:15

5 Answers 5

19

Try this:

var MyArray = [];
MyArray[StringVariableName] = MyArray[StringVariableName] || [];
MyArray[StringVariableName][StringVariableName2] = MyArray[StringVariableName][StringVariableName2] || [];
MyArray[StringVariableName][StringVariableName2].push("whatever");
Sign up to request clarification or add additional context in comments.

3 Comments

Works amazing, can you explain what that assignment means?
@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.
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.
4

You could even, through the power of expressions, do this with a one-liner.

(MyArray[StringVariableName][StringVariableName2] || (MyArray[StringVariableName][StringVariableName2] = [])).push("whatever");

1 Comment

This is awesome. Is there any performance lag in this approach?
0

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

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

myObj.StringVariableName.StringVariableName2.push("whatever");

1 Comment

You are doing it wrong way because StringVariableName looks like actually a variable name. You are making them as constant keys in the object.
0

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");

2 Comments

.push() is for arrays, not objects.
@A__ Both of us are correct as myObject[str1][str2] is an array and not an object here.
0

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

https://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

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

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

if you have any questions just ask.

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.