Passing a variable to a string that is inserted into an existing array

I am trying to pass a variable to a string, while the string itself is inserted into an existing array as follows:

var myVar = 'slug'; myArray.push(['item one', '/path/to/' + myVar + '/']); 

This does not work at all. I see that the correct value is assigned to myVar ; however, myVar in the array is not even recognized as a variable.

It seems simple enough, but I obviously missed something.

Thank you for your help.

EDIT: not a Javascript issue.

As Alex showed, this definitely works fine. (And yes, this is an existing array.)

However, I forgot to mention that the push method is wrapped in a bit of Django template logic so that it only runs on my dev instance, for example:

 {% ifequal INSTANCE 'DEVELOPMENT' %] myArray.push(['item one', '/path/to/' + myVar + '/']); {% endifequal %} 

I did not mention logic because I did not see how this could interfere. I have confirmed that:

  • everything with logic behaved as expected
  • INSTANCE really equaled "DEVELOPMENT"
  • and this Javascript line was visible in the source, which would not be if the logic were bad.

However, if I remove this Django logic, everything with Javascript will work as desired.

For some reason, this server-side logic is stopping me from passing any external values ​​to the array, on the client or server side.

As an experiment, I tried this ...

 myArray.push(['item one', '/path/to/{{ block slug }}undefined{{ endblock }}/']); 

... with the following in the child template.

 {{ block slug }}slug{{ endblock }} 

The same thing is happening. When using template logic, the value is /path/to/undefined . Without it, the value /path/to/slug is optional.

Why this happens is a mystery to me. I can get around this, but if anyone has any thoughts, I would like to hear them.

Thanks again.


Noob side question: what is etiquette about changing message headers?

+4
source share
3 answers

It definitely works .

Perhaps you decided to combine Array instead of concat() ? At the moment, you are pushing a new Array onto an existing Array as a member, resulting in a multi-dimensional Array .

I also suggested that myArray already pointed to Array . If not, just var myArray = [] .

+3
source

You need to instantiate the array before using the push method:

 var myVar = 'slug'; var myArray=new Array(); myArray.push(['item one', '/path/to/' + myVar + '/']); 
0
source

What does not work?

You create an array inside the array.

You need to access new elements of arrays, for example:

 myArray[0][1] // this is "/paht/to/slug/ 

After re-reading your comments, it seems like you are trying to access an array property:

 myArray.myVar 

In javascript, if you want to have a named element, you need to use an object.

 myObject = {}; myObject.myVar = "/path/to/slug/"; alert(myObject.myVar) // "/path/to/slug/" 

Or you can add it directly to the array as a property:

 myArray.myVar = "/path/to/slug/"; alert(myArray.myVar); 
0
source

All Articles