1

Topic: Indirect addressing in javascript

Actually, the question sounds so:
- How in JS to address to to contents (to read or change it) a variable which name is in advance not known, but it is known, what it will be in another (known) variable?
For example, in a certain cycle from 1 to 9 program names of variables are formed:

 [i] stroka1..., stroka9; [/i] 

and right after - in a variable with this name it is necessary to add formations of each name a certain collecting line. Then, after that cycle in the further text of the program it will be necessary to address few times to contents of each such variable. Probably, for such reversal - it will be better to tire out the created aforementioned names of variables in the intermediate array.
Whether it is possible to make it means JS? Prompt, please.

2

Re: Indirect addressing in javascript

I do not want to give the bad councils therefore I will ask, what for variables?
Still the question and to what it is possible addresses except as to variable contents?
It is better to describe the task, than to invent the strange decision of the task

3

Re: Indirect addressing in javascript

I did not get a grasp that is necessary to you, but can type such

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push ("Kiwi");
var energy = fruits.join ("and");

4

Re: Indirect addressing in javascript

http://javascript.ru/eval

5

Re: Indirect addressing in javascript

neznajka wrote:

 stroka1..., stroka9; 

It seems to me, all these "variables" a place in a unique array

6

Re: Indirect addressing in javascript

2012;
eval it is that bad advice

7

Re: Indirect addressing in javascript

Var79 wrote:

eval it is that bad advice

not safely...
Council not the harmful.
The author a topic that has a site which hackers dream to break?
Most likely while is not present...
And when studies, in all understands.

8

Re: Indirect addressing in javascript

Var79;
Strange can also - but everyone solves problems, being based on any base already known to it. To me it is known such, and I while only study base JS, therefore I ask here. Probably, in JS there is also the best method.
Was more specific - approximately so. In a cycle names of indexed variables (probably better - the associative array) in which the HTML-code formed in the same cycle will collect are formed. The code is self-sufficient elements of the web page. In the further ambassador of a cycle - depending on exterior conditions all (or some) these elements will be deduced to the visitor on page in this or that order.
Was even more specific - I try to generate now on a test platform. As I will issue more or less intelligibly - I will lay out the link.

9

Re: Indirect addressing in javascript

neznajka wrote:

For example, in a certain cycle from 1 to 9 program names of variables are formed:

 [i] stroka1..., stroka9; [/i] 

and right after - in a variable with this name it is necessary to add formations of each name a certain collecting line. Then, after that cycle in the further text of the program it will be necessary to address few times to contents of each such variable.

For the decision of such tasks there are arrays...

neznajka wrote:

- as in JS to address to to contents (to read or change it) a variable which name is in advance not known, but it is known, what it will be in another (known) variable?

For all remaining there are objects. There under different "keys" it is possible to store different contents.
And reversal to keys probably by name

var obj = {
test: ' the Example'
};
var key ='test ';
alert (obj [key]);

10

Re: Indirect addressing in javascript

krvsa, THANKS!
The pancake, I somehow also did not think of objects...
It seems, it approaches me smile

11

Re: Indirect addressing in javascript

neznajka;
No indexed variables are necessary, I resulted the code

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push ("Kiwi");
var energy = fruits.join ("and");//or fruits.join () if without a separator
alert (energy);

12

Re: Indirect addressing in javascript

neznajka;
At objects is not present.join ()

13

Re: Indirect addressing in javascript

Not always it is possible to use arrays. For example, from page.evaluate it is impossible to return an array. In that case use JSON-serialization.

14

Re: Indirect addressing in javascript

To all:
M-yes... I here thought more carefully - and can to me indeed it is not necessary fence such "kitchen garden"? Perhaps all dares easier...
More shortly, the idea arose the following.
On page is available 2 tree-like uncovered lists - disclosure and design work normally and do not concern a question essence, therefore I result only a html-code in "a bare type".
If to copy the given code in  the editor it is possible to see that it very much even the regular. I.e. - this code easily can be generated in a cycle (since all numeral values of this tree too are calculated by the simple formula which is easy for anchoring to a cycle step). But there are 2 essential moments:
1) the code of trees though is simple, but is volume enough, especially - 2 tree (it is longer, but structurally - same and consequently I do not result it);
2) necessity of show of 1st or 2nd tree on page - appears no means always and depends on actions of the user (whether he wants to change offered on-default, or whether he wants to enter the value necessary to it manually, or prefers to select standard value from the list);
Here also there was an idea - not to drive from the server such infrequently demanded but easily formed html-code, and to form it on client side only in case of need.
Here also I seek a method to make it "more gracefully" smile

15

Re: Indirect addressing in javascript

neznajka;
Use templates, for example {{handlebars}} .
Describe as will be the tree on typical structure is under construction, during the moment when the tree is necessary feed to a template the data, it  html which implement in page.
I recommend because the decision is checked up, used in some  and disaccustoms  in a template, i.e. at once get used to the correct philosophy. In the presence of the collector templates can be recompiled at once in js a file.

16

Re: Indirect addressing in javascript

To :
I and did till now. But - I want to develop further smile
To mage.lan:
Thanks! I about such compiled templates did not know.

17

Re: Indirect addressing in javascript

mage.lan wrote:

Use templates, for example {{handlebars}} .

+1
The interesting link

18

Re: Indirect addressing in javascript

neznajka wrote:

krvsa, THANKS!
The pancake, I somehow also did not think of objects...

Funny smile
Objects in JavaScript combine two important functionals.
The first is the associative array: structure, suitable for storage of any data...
https://learn.javascript.ru/object

19

Re: Indirect addressing in javascript

https://learn.javascript.ru/templates
https://learn.javascript.ru/template-lodash
https://learn.javascript.ru/template-tag

20

Re: Indirect addressing in javascript

Thanks, skyANA, - only not so quickly and was abundantly, please: I have not time to digest smile
Joke.

21

Re: Indirect addressing in javascript

wrote:

it is passed...
If only for development, and so for normal operation it  the decision. All remaining from the crafty.

. Give again on 20 pages.
The first flame items is fulfilled. A phrase about what it is said.

22

Re: Indirect addressing in javascript

wrote:

Petro123;
As on you you will always find an occasion;
And a practical question answer

Like you above tried about architecture and  that that to tell).

23

Re: Indirect addressing in javascript

skyANA;
Thanks once again for the link about objects.
But there was a question connected to it.
By means of object as there it is shown, normally dares and indirect addressing interesting me yesterday, and some questions of multidimentional arrays.
In general, yesterday played with objects, and now - it is impossible to work with a 2-dimensional array without application of objects sad
In particular - here test function:

function proverka ()
{var arr = [];
for (var i = 0; i <10; i ++)
{for (var j = 0; j <10; j ++)
{arr [i] [j] =i.toString + ' _ ' + j.toString;}//here swears on indexes :(
}
console.log (arr);
}

I tried and here so to declare: var arr = new Array (10); - all the same swears on 2-dimensional indexes at attempt to appropriate value.
In the Internet examples - operation with the multidimentional arrays created from one-dimensional (in advance filled) is described. And if it is in advance not known neither an amount of one-dimensional arrays, nor an amount of elements in each of them - whether that it is possible to use purely "large" methods somehow? Or all the same it is better to use object with properties? In my case it is known that the amount of one-dimensional arrays in 2-dimensional does not exceed 10, and the amount of elements in each of them - does not exceed 20.

24

Re: Indirect addressing in javascript

About, found !
It appears: [quote =] In JavaScript if we want to create a multidimentional array it is necessary to specify explicitly each time that the given array cell in turn too is an array.

M-M... Live and learn! smile