user27 user27 - 5 months ago 8
Javascript Question

Remove comma from java script array

I want to know how to remove comma from below list array elements, my code:

var lUserNames= [];
$.each(lUsers, function( index, value ) {
lUserNames.push("<li>" + lUsers[index].name + "</li>");
lUserNames.join("");
});


Output with above code:

. User1
,
. User2
,
. User3


Request: I tried join in all ways, please don't mark as duplicate blindly pointing to other post.

Answer

Apply Array#join after $.each loop

var lUserNames = [];
var lUsers = [1, 2, 3, 4];
$.each(lUsers, function() {
  lUserNames.push("<li>" + this + "</li>");
});
var op = lUserNames.join("");
console.log(op);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>