'Join associative arrays in Javascript

I have two (or more) associative arrays in Javascript:

tagArray['title'] = ('<H1>title</H1>');
tagArray['text'] = ('<P>text</P>');

And want to join them like:

tagFinal = tagArray.join("<BR>");

But the result is empty.

It should result in: tagFinal = '<H1>title</H1><BR><P>text</P>';

What am I doing wrong? (I also tried without the tags, no difference) Or am I better off push()-ng it to a new array/string?



Solution 1:[1]

This code should work for you:

var tagFinal = "";
var i = 0;
    
for (var key in tagArray) {
    if(i == 0){separator = "";}else{separator = "<br>";}
    tagFinal += separator + tagArray[key];
    i++;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Enrico Maria Governatori