4

In javascript loop i am generating a xml like this:

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(' '); 
    }

Forgot about the t variable and all. by run this code i am getting the s value in the following format:

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

In the second iteration it shows like this:

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

and so on till the m<t.length satisfy. What i want to join the list in each iteration. After joining all of them i should get in this way:

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

I tried to do it with join written in comment section, but didn't work for me. What i am doing wrong?

2
  • Join merges an array into a string. This is not what you're after. Use + or += to append stuff to your s variable. Commented Sep 3, 2012 at 22:16
  • @zneak I am beginner in java script, but i tried with join but it didn't work for me Commented Sep 3, 2012 at 22:17

1 Answer 1

2

The best way would be to define a string outside the loop an append 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(), make v an array and push() elements on to it (note that join() is an array method, not for string's)

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'll be pleased to see I've tried to adhere to your variable naming conventions in my examples (i.e. meaningless characters).

Sign up to request clarification or add additional context in comments.

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.