How to use connection (inline method) in javascript?

In javascript loop, I create xml as follows:

 for (m = 0; m < t.length; m++) { var arr1 = t[m].split("\t"); s = "'<row dc= '" + arr1[0] + "' al='" + arr1[1] + "' msg='" + arr1[2] + "' />'"; alert(s); //s = s.join(' '); } 

I forgot about the variable t and all. by running this code, I get the s value in the following format:

 <row dc = "abc" al="56" msg="dkaj" /> 

In the second iteration, it looks like this:

 <row dc = "abwwc" al="56w" msg="dkajad" /> 

etc. to m<t.length . I want to join the list at each iteration. After I joined them, I have to do this:

 <row dc = "abc" al="56" msg="dkaj" /><row dc = "abwwc" al="56w" msg="dkajad" /> and so on.. 

I tried to do this by joining the entry in the comments section, but didn't work for me. What am I doing wrong?

+4
source share
1 answer

A better way would be to define a line outside the loop by adding to it:

 var v = ''; for (m = 0; m < t.length; m++) { var arr1 = t[m].split("\t"); s = "'<row dc= '" + arr1[0] + "' al='" + arr1[1] + "' msg='" + arr1[2] + "' />'"; alert(s); v += s; } alert(v); 

If you still want to use join() , insert v array elements and push() (note that join() is an array , not a string)

 var y = []; for (m = 0; m < t.length; m++) { var arr1 = t[m].split("\t"); s = "'<row dc= '" + arr1[0] + "' al='" + arr1[1] + "' msg='" + arr1[2] + "' />'"; alert(s); y.push(s); } alert(y.join('')); 

You will be glad to see that I tried to stick to your variable name conventions in my examples (i.e. meaningless characters).

+2
source

Source: https://habr.com/ru/post/1432161/


All Articles