0

I am having trouble finding the correct wording to find the answer. I am trying to combine elements in an array.

 array[0] = "bob";
 array[1] = "john";
 array[2] = "frank";

 for (i = 0;i<array.length;i++)
 {
 (in php i would use .=)
     var list .= array[i]+"<br>";
 }

What do i replace .= with?

3
  • 2
    += in Javascript is the same as .= in php. You just need to be careful with ints and strings Commented May 16, 2013 at 22:05
  • Also you forgot to declare i. Commented May 16, 2013 at 22:14
  • look at my answer, I give you 2 options to solve it. Commented May 16, 2013 at 22:33

2 Answers 2

4

You can just use .join()

array.join('</br>')
Sign up to request clarification or add additional context in comments.

3 Comments

In php as well (implode), no loops required.
</br>? Are you sure about that? I hope you meant: <br />
yup.. </br> or <br /> or <br> should do.. but eventually is depends on DOCTYPE though..
1

This is what you're looking for:

 array[0] = "bob";
 array[1] = "john";
 array[2] = "frank";
 array.join('</br>')

And if you do want to keep with your loop then use the += operator:

 var list = '';
 for (var i = 0; i < array.length; i++)
 {
     list += array[i]+"<br/>";
 }

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.