wyc
wyc

Reputation: 55273

How do I get rid of the commas in the following JavaScript loop?

The code:

var fileChapters = (function() {
  var results = [];
  for (i = 0, len = posts.length; i < len; _i++) {
    var post = posts[i];
    results.push("<p><a href=\"#chap1\">" + post.title + "</a></p>\n\n");
  }
  return results;
});

console log to fileChapters:

["<p><a href="#chap1">Post 1</a></p>↵↵", "<p><a href="#chap1">Post 2</a></p>↵↵", "<p><a href="#chap1">Untitled</a></p>↵↵", "<p><a href="#chap1">Untitled</a></p>↵↵", "<p><a href="#chap1">Untitled</a></p>↵↵"]

Now when I do push:

file.unshift fileTOC

I end up with the following HTML:

  <p><a href="#chap1">Post 1</a></p>

,<p><a href="#chap1">Post 2</a></p>

,<p><a href="#chap1">Untitled</a></p>

,<p><a href="#chap1">Untitled</a></p>

,<p><a href="#chap1">Untitled</a></p>

What's happening here? And how to solve it?

Upvotes: 0

Views: 49

Answers (1)

epascarello
epascarello

Reputation: 207501

It is an array, it has commas when you do toString()

If you do not want the commas, use

return results.join("");

Upvotes: 3

Related Questions