user27
user27

Reputation: 274

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

I've tried several methods for joining, none of which worked.

Upvotes: 1

Views: 114

Answers (1)

Rayon
Rayon

Reputation: 36609

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>

Upvotes: 2

Related Questions