how to add commas between names in an array using join method-Javascript





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















This may be super dumb question, but need help on this.
How can I join the names of the users below and separate it by ","?



code:



var users = ["fer", "jyujyu", "jhwevhfwfv"]

<div>
{users.map(function(user){
return <span key={user}className="user">{user}</span>
})}
</div>


Where can I apply the join here to add commas between names:
expected o/p:



fer, jyujyu, jhwevhfwfv









share|improve this question

























  • <span key={user}className="user">{user} , </span> will work but adds extra , after last. So you just need to handle that using if else or last.

    – Atul Sharma
    Jul 30 '18 at 5:36




















1















This may be super dumb question, but need help on this.
How can I join the names of the users below and separate it by ","?



code:



var users = ["fer", "jyujyu", "jhwevhfwfv"]

<div>
{users.map(function(user){
return <span key={user}className="user">{user}</span>
})}
</div>


Where can I apply the join here to add commas between names:
expected o/p:



fer, jyujyu, jhwevhfwfv









share|improve this question

























  • <span key={user}className="user">{user} , </span> will work but adds extra , after last. So you just need to handle that using if else or last.

    – Atul Sharma
    Jul 30 '18 at 5:36
















1












1








1








This may be super dumb question, but need help on this.
How can I join the names of the users below and separate it by ","?



code:



var users = ["fer", "jyujyu", "jhwevhfwfv"]

<div>
{users.map(function(user){
return <span key={user}className="user">{user}</span>
})}
</div>


Where can I apply the join here to add commas between names:
expected o/p:



fer, jyujyu, jhwevhfwfv









share|improve this question
















This may be super dumb question, but need help on this.
How can I join the names of the users below and separate it by ","?



code:



var users = ["fer", "jyujyu", "jhwevhfwfv"]

<div>
{users.map(function(user){
return <span key={user}className="user">{user}</span>
})}
</div>


Where can I apply the join here to add commas between names:
expected o/p:



fer, jyujyu, jhwevhfwfv






javascript reactjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 30 '18 at 5:48









mplungjan

90.6k22127186




90.6k22127186










asked Jul 30 '18 at 5:35









user1234user1234

1,05421641




1,05421641













  • <span key={user}className="user">{user} , </span> will work but adds extra , after last. So you just need to handle that using if else or last.

    – Atul Sharma
    Jul 30 '18 at 5:36





















  • <span key={user}className="user">{user} , </span> will work but adds extra , after last. So you just need to handle that using if else or last.

    – Atul Sharma
    Jul 30 '18 at 5:36



















<span key={user}className="user">{user} , </span> will work but adds extra , after last. So you just need to handle that using if else or last.

– Atul Sharma
Jul 30 '18 at 5:36







<span key={user}className="user">{user} , </span> will work but adds extra , after last. So you just need to handle that using if else or last.

– Atul Sharma
Jul 30 '18 at 5:36














4 Answers
4






active

oldest

votes


















0














You do not need to map if you just want to display all the items in the array.



var users = ["fer", "jyujyu", "jhwevhfwfv"]



using array join you should be able to display all items in a span



users.join(', ') will give you the expected output, this joins all items of the array with what you specified and it returns a string.



putting everything together.



var users = ["fer", "jyujyu", "jhwevhfwfv"]

<div>
{
<span className="user">{users.join(', ')}</span>
}
</div>


here is the link to the sandbox of this working https://codesandbox.io/s/znrj1nopox






share|improve this answer































    1














    Here's a simple way to do with reduce:



    var users = ["fer", "jyujyu", "jhwevhfwfv"]
    <div>
    {
    users
    .map(function(user){
    return <span key={user} className="user">{user}</span>
    })
    .reduce(function(prev, next){
    if(Array.isArray(prev))
    return prev.concat(<span>, </span>).concat(next)
    else return [prev, (<span>, </span>), next]
    })
    }
    </div>;


    Here is a codepen example






    share|improve this answer


























    • That looks wonky. Why not users .map(function(user){ return `<span key=${user}className="user">${user}</span>` }) .join(",")

      – mplungjan
      Jul 30 '18 at 5:51













    • You can do it that way. But remember that your comma has no element tag.

      – Terry Wei
      Jul 30 '18 at 5:52











    • I assume your code is React - will the return really work like that?

      – mplungjan
      Jul 30 '18 at 5:53











    • If not React, your JS in not valid is what I meant to say.

      – mplungjan
      Jul 30 '18 at 5:59











    • @mplungjan: yes Im able to return the user names and list them, however with just space and no commas, I wanted to insert commas in between

      – user1234
      Jul 30 '18 at 6:05



















    1














    let numbers = [1, 2, 3].join(",").split("");

    const listItems = numbers.map(number => (
    <span key={number.toString()}>{number}</span>
    ));
    console.log(listItems);
    return <div>{listItems}</div>;


    I have updated the answer. Since join converts array into string we cant directly render in react component. i have converted to an array to avoid this problem. See the below working example:
    https://codesandbox.io/s/ymvl8olpxx






    share|improve this answer


























    • He wants to wrap in spans

      – mplungjan
      Jul 30 '18 at 5:52











    • included the span wrapping.

      – Srinivas
      Jul 30 '18 at 6:07











    • Shouldn't it be ${user} ?

      – Suhas
      Jul 30 '18 at 6:08











    • Will this generate <span> elements or will it generate the string "<span>"?

      – Code-Apprentice
      Jul 30 '18 at 6:12











    • @suhas u r right, its ${user}

      – Srinivas
      Jul 30 '18 at 6:14



















    -1














    I would try to do something like so (built upon a previous answer):



    var users = ["fer", "jyujyu", "jhwevhfwfv"];
    var spannedUsers = users.map((user) => <span key={user} className="user">{user}</span>);
    <div>
    {spannedUsers.join(", ")}
    </div>





    share|improve this answer


























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51587522%2fhow-to-add-commas-between-names-in-an-array-using-join-method-javascript%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You do not need to map if you just want to display all the items in the array.



      var users = ["fer", "jyujyu", "jhwevhfwfv"]



      using array join you should be able to display all items in a span



      users.join(', ') will give you the expected output, this joins all items of the array with what you specified and it returns a string.



      putting everything together.



      var users = ["fer", "jyujyu", "jhwevhfwfv"]

      <div>
      {
      <span className="user">{users.join(', ')}</span>
      }
      </div>


      here is the link to the sandbox of this working https://codesandbox.io/s/znrj1nopox






      share|improve this answer




























        0














        You do not need to map if you just want to display all the items in the array.



        var users = ["fer", "jyujyu", "jhwevhfwfv"]



        using array join you should be able to display all items in a span



        users.join(', ') will give you the expected output, this joins all items of the array with what you specified and it returns a string.



        putting everything together.



        var users = ["fer", "jyujyu", "jhwevhfwfv"]

        <div>
        {
        <span className="user">{users.join(', ')}</span>
        }
        </div>


        here is the link to the sandbox of this working https://codesandbox.io/s/znrj1nopox






        share|improve this answer


























          0












          0








          0







          You do not need to map if you just want to display all the items in the array.



          var users = ["fer", "jyujyu", "jhwevhfwfv"]



          using array join you should be able to display all items in a span



          users.join(', ') will give you the expected output, this joins all items of the array with what you specified and it returns a string.



          putting everything together.



          var users = ["fer", "jyujyu", "jhwevhfwfv"]

          <div>
          {
          <span className="user">{users.join(', ')}</span>
          }
          </div>


          here is the link to the sandbox of this working https://codesandbox.io/s/znrj1nopox






          share|improve this answer













          You do not need to map if you just want to display all the items in the array.



          var users = ["fer", "jyujyu", "jhwevhfwfv"]



          using array join you should be able to display all items in a span



          users.join(', ') will give you the expected output, this joins all items of the array with what you specified and it returns a string.



          putting everything together.



          var users = ["fer", "jyujyu", "jhwevhfwfv"]

          <div>
          {
          <span className="user">{users.join(', ')}</span>
          }
          </div>


          here is the link to the sandbox of this working https://codesandbox.io/s/znrj1nopox







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 30 '18 at 7:06









          Olivier JMOlivier JM

          1517




          1517

























              1














              Here's a simple way to do with reduce:



              var users = ["fer", "jyujyu", "jhwevhfwfv"]
              <div>
              {
              users
              .map(function(user){
              return <span key={user} className="user">{user}</span>
              })
              .reduce(function(prev, next){
              if(Array.isArray(prev))
              return prev.concat(<span>, </span>).concat(next)
              else return [prev, (<span>, </span>), next]
              })
              }
              </div>;


              Here is a codepen example






              share|improve this answer


























              • That looks wonky. Why not users .map(function(user){ return `<span key=${user}className="user">${user}</span>` }) .join(",")

                – mplungjan
                Jul 30 '18 at 5:51













              • You can do it that way. But remember that your comma has no element tag.

                – Terry Wei
                Jul 30 '18 at 5:52











              • I assume your code is React - will the return really work like that?

                – mplungjan
                Jul 30 '18 at 5:53











              • If not React, your JS in not valid is what I meant to say.

                – mplungjan
                Jul 30 '18 at 5:59











              • @mplungjan: yes Im able to return the user names and list them, however with just space and no commas, I wanted to insert commas in between

                – user1234
                Jul 30 '18 at 6:05
















              1














              Here's a simple way to do with reduce:



              var users = ["fer", "jyujyu", "jhwevhfwfv"]
              <div>
              {
              users
              .map(function(user){
              return <span key={user} className="user">{user}</span>
              })
              .reduce(function(prev, next){
              if(Array.isArray(prev))
              return prev.concat(<span>, </span>).concat(next)
              else return [prev, (<span>, </span>), next]
              })
              }
              </div>;


              Here is a codepen example






              share|improve this answer


























              • That looks wonky. Why not users .map(function(user){ return `<span key=${user}className="user">${user}</span>` }) .join(",")

                – mplungjan
                Jul 30 '18 at 5:51













              • You can do it that way. But remember that your comma has no element tag.

                – Terry Wei
                Jul 30 '18 at 5:52











              • I assume your code is React - will the return really work like that?

                – mplungjan
                Jul 30 '18 at 5:53











              • If not React, your JS in not valid is what I meant to say.

                – mplungjan
                Jul 30 '18 at 5:59











              • @mplungjan: yes Im able to return the user names and list them, however with just space and no commas, I wanted to insert commas in between

                – user1234
                Jul 30 '18 at 6:05














              1












              1








              1







              Here's a simple way to do with reduce:



              var users = ["fer", "jyujyu", "jhwevhfwfv"]
              <div>
              {
              users
              .map(function(user){
              return <span key={user} className="user">{user}</span>
              })
              .reduce(function(prev, next){
              if(Array.isArray(prev))
              return prev.concat(<span>, </span>).concat(next)
              else return [prev, (<span>, </span>), next]
              })
              }
              </div>;


              Here is a codepen example






              share|improve this answer















              Here's a simple way to do with reduce:



              var users = ["fer", "jyujyu", "jhwevhfwfv"]
              <div>
              {
              users
              .map(function(user){
              return <span key={user} className="user">{user}</span>
              })
              .reduce(function(prev, next){
              if(Array.isArray(prev))
              return prev.concat(<span>, </span>).concat(next)
              else return [prev, (<span>, </span>), next]
              })
              }
              </div>;


              Here is a codepen example







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jul 30 '18 at 6:57

























              answered Jul 30 '18 at 5:42









              Terry WeiTerry Wei

              1,356616




              1,356616













              • That looks wonky. Why not users .map(function(user){ return `<span key=${user}className="user">${user}</span>` }) .join(",")

                – mplungjan
                Jul 30 '18 at 5:51













              • You can do it that way. But remember that your comma has no element tag.

                – Terry Wei
                Jul 30 '18 at 5:52











              • I assume your code is React - will the return really work like that?

                – mplungjan
                Jul 30 '18 at 5:53











              • If not React, your JS in not valid is what I meant to say.

                – mplungjan
                Jul 30 '18 at 5:59











              • @mplungjan: yes Im able to return the user names and list them, however with just space and no commas, I wanted to insert commas in between

                – user1234
                Jul 30 '18 at 6:05



















              • That looks wonky. Why not users .map(function(user){ return `<span key=${user}className="user">${user}</span>` }) .join(",")

                – mplungjan
                Jul 30 '18 at 5:51













              • You can do it that way. But remember that your comma has no element tag.

                – Terry Wei
                Jul 30 '18 at 5:52











              • I assume your code is React - will the return really work like that?

                – mplungjan
                Jul 30 '18 at 5:53











              • If not React, your JS in not valid is what I meant to say.

                – mplungjan
                Jul 30 '18 at 5:59











              • @mplungjan: yes Im able to return the user names and list them, however with just space and no commas, I wanted to insert commas in between

                – user1234
                Jul 30 '18 at 6:05

















              That looks wonky. Why not users .map(function(user){ return `<span key=${user}className="user">${user}</span>` }) .join(",")

              – mplungjan
              Jul 30 '18 at 5:51







              That looks wonky. Why not users .map(function(user){ return `<span key=${user}className="user">${user}</span>` }) .join(",")

              – mplungjan
              Jul 30 '18 at 5:51















              You can do it that way. But remember that your comma has no element tag.

              – Terry Wei
              Jul 30 '18 at 5:52





              You can do it that way. But remember that your comma has no element tag.

              – Terry Wei
              Jul 30 '18 at 5:52













              I assume your code is React - will the return really work like that?

              – mplungjan
              Jul 30 '18 at 5:53





              I assume your code is React - will the return really work like that?

              – mplungjan
              Jul 30 '18 at 5:53













              If not React, your JS in not valid is what I meant to say.

              – mplungjan
              Jul 30 '18 at 5:59





              If not React, your JS in not valid is what I meant to say.

              – mplungjan
              Jul 30 '18 at 5:59













              @mplungjan: yes Im able to return the user names and list them, however with just space and no commas, I wanted to insert commas in between

              – user1234
              Jul 30 '18 at 6:05





              @mplungjan: yes Im able to return the user names and list them, however with just space and no commas, I wanted to insert commas in between

              – user1234
              Jul 30 '18 at 6:05











              1














              let numbers = [1, 2, 3].join(",").split("");

              const listItems = numbers.map(number => (
              <span key={number.toString()}>{number}</span>
              ));
              console.log(listItems);
              return <div>{listItems}</div>;


              I have updated the answer. Since join converts array into string we cant directly render in react component. i have converted to an array to avoid this problem. See the below working example:
              https://codesandbox.io/s/ymvl8olpxx






              share|improve this answer


























              • He wants to wrap in spans

                – mplungjan
                Jul 30 '18 at 5:52











              • included the span wrapping.

                – Srinivas
                Jul 30 '18 at 6:07











              • Shouldn't it be ${user} ?

                – Suhas
                Jul 30 '18 at 6:08











              • Will this generate <span> elements or will it generate the string "<span>"?

                – Code-Apprentice
                Jul 30 '18 at 6:12











              • @suhas u r right, its ${user}

                – Srinivas
                Jul 30 '18 at 6:14
















              1














              let numbers = [1, 2, 3].join(",").split("");

              const listItems = numbers.map(number => (
              <span key={number.toString()}>{number}</span>
              ));
              console.log(listItems);
              return <div>{listItems}</div>;


              I have updated the answer. Since join converts array into string we cant directly render in react component. i have converted to an array to avoid this problem. See the below working example:
              https://codesandbox.io/s/ymvl8olpxx






              share|improve this answer


























              • He wants to wrap in spans

                – mplungjan
                Jul 30 '18 at 5:52











              • included the span wrapping.

                – Srinivas
                Jul 30 '18 at 6:07











              • Shouldn't it be ${user} ?

                – Suhas
                Jul 30 '18 at 6:08











              • Will this generate <span> elements or will it generate the string "<span>"?

                – Code-Apprentice
                Jul 30 '18 at 6:12











              • @suhas u r right, its ${user}

                – Srinivas
                Jul 30 '18 at 6:14














              1












              1








              1







              let numbers = [1, 2, 3].join(",").split("");

              const listItems = numbers.map(number => (
              <span key={number.toString()}>{number}</span>
              ));
              console.log(listItems);
              return <div>{listItems}</div>;


              I have updated the answer. Since join converts array into string we cant directly render in react component. i have converted to an array to avoid this problem. See the below working example:
              https://codesandbox.io/s/ymvl8olpxx






              share|improve this answer















              let numbers = [1, 2, 3].join(",").split("");

              const listItems = numbers.map(number => (
              <span key={number.toString()}>{number}</span>
              ));
              console.log(listItems);
              return <div>{listItems}</div>;


              I have updated the answer. Since join converts array into string we cant directly render in react component. i have converted to an array to avoid this problem. See the below working example:
              https://codesandbox.io/s/ymvl8olpxx







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Aug 1 '18 at 11:00

























              answered Jul 30 '18 at 5:39









              SrinivasSrinivas

              60331127




              60331127













              • He wants to wrap in spans

                – mplungjan
                Jul 30 '18 at 5:52











              • included the span wrapping.

                – Srinivas
                Jul 30 '18 at 6:07











              • Shouldn't it be ${user} ?

                – Suhas
                Jul 30 '18 at 6:08











              • Will this generate <span> elements or will it generate the string "<span>"?

                – Code-Apprentice
                Jul 30 '18 at 6:12











              • @suhas u r right, its ${user}

                – Srinivas
                Jul 30 '18 at 6:14



















              • He wants to wrap in spans

                – mplungjan
                Jul 30 '18 at 5:52











              • included the span wrapping.

                – Srinivas
                Jul 30 '18 at 6:07











              • Shouldn't it be ${user} ?

                – Suhas
                Jul 30 '18 at 6:08











              • Will this generate <span> elements or will it generate the string "<span>"?

                – Code-Apprentice
                Jul 30 '18 at 6:12











              • @suhas u r right, its ${user}

                – Srinivas
                Jul 30 '18 at 6:14

















              He wants to wrap in spans

              – mplungjan
              Jul 30 '18 at 5:52





              He wants to wrap in spans

              – mplungjan
              Jul 30 '18 at 5:52













              included the span wrapping.

              – Srinivas
              Jul 30 '18 at 6:07





              included the span wrapping.

              – Srinivas
              Jul 30 '18 at 6:07













              Shouldn't it be ${user} ?

              – Suhas
              Jul 30 '18 at 6:08





              Shouldn't it be ${user} ?

              – Suhas
              Jul 30 '18 at 6:08













              Will this generate <span> elements or will it generate the string "<span>"?

              – Code-Apprentice
              Jul 30 '18 at 6:12





              Will this generate <span> elements or will it generate the string "<span>"?

              – Code-Apprentice
              Jul 30 '18 at 6:12













              @suhas u r right, its ${user}

              – Srinivas
              Jul 30 '18 at 6:14





              @suhas u r right, its ${user}

              – Srinivas
              Jul 30 '18 at 6:14











              -1














              I would try to do something like so (built upon a previous answer):



              var users = ["fer", "jyujyu", "jhwevhfwfv"];
              var spannedUsers = users.map((user) => <span key={user} className="user">{user}</span>);
              <div>
              {spannedUsers.join(", ")}
              </div>





              share|improve this answer






























                -1














                I would try to do something like so (built upon a previous answer):



                var users = ["fer", "jyujyu", "jhwevhfwfv"];
                var spannedUsers = users.map((user) => <span key={user} className="user">{user}</span>);
                <div>
                {spannedUsers.join(", ")}
                </div>





                share|improve this answer




























                  -1












                  -1








                  -1







                  I would try to do something like so (built upon a previous answer):



                  var users = ["fer", "jyujyu", "jhwevhfwfv"];
                  var spannedUsers = users.map((user) => <span key={user} className="user">{user}</span>);
                  <div>
                  {spannedUsers.join(", ")}
                  </div>





                  share|improve this answer















                  I would try to do something like so (built upon a previous answer):



                  var users = ["fer", "jyujyu", "jhwevhfwfv"];
                  var spannedUsers = users.map((user) => <span key={user} className="user">{user}</span>);
                  <div>
                  {spannedUsers.join(", ")}
                  </div>






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 4 at 13:21

























                  answered Jul 30 '18 at 6:07









                  SuhasSuhas

                  342110




                  342110






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51587522%2fhow-to-add-commas-between-names-in-an-array-using-join-method-javascript%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas