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?
asked May 16, 2013 at 22:04
bart2puck
2,5183 gold badges33 silver badges61 bronze badges
2 Answers 2
You can just use .join()
array.join('</br>')
answered May 16, 2013 at 22:06
Sushanth --
55.8k9 gold badges70 silver badges109 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
bfavaretto
In php as well (
implode), no loops required.David Thomas
</br>? Are you sure about that? I hope you meant: <br />Sushanth --
yup..
</br> or <br /> or <br> should do.. but eventually is depends on DOCTYPE though..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/>";
}
answered May 16, 2013 at 22:12
Yair Nevet
13.1k17 gold badges71 silver badges111 bronze badges
Comments
lang-js
i.