1

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.

1
  • How can i remove that comma? @Rayon Commented Jul 4, 2016 at 12:02

1 Answer 1

2

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>

Sign up to request clarification or add additional context in comments.

1 Comment

I did not assign it to another variable after using join, Its working now @Rayon

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.