SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    How do I get result like this from a javascript array?

    Hi,

    I'm trying to do something like this with several methods unsuccessfully:
    Code:
    var array = "0, 1, 2, 3, 4, 5, 6, 7, 8, 9";
    And I want the result like this:
    Code:
    "0123456789"
    In ascending order, how do I achieve this?

    Thanks

  2. #2
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use a regular expression to achieve this.

    var array = "0, 1, 2, 3, 4, 5, 6, 7, 8, 9";
    var new_array = array.replace(/,\s/g, "");

    This assumes that your original string is already in ascending order.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    If it were an actual array that you start with, then your desired task would be simple and easy.

    Code javascript:
    var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

    Are you sure that that's not what you're wanting to start with?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    If it were an actual array that you start with, then your desired task would be simple and easy.

    Code javascript:
    var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

    Are you sure that that's not what you're wanting to start with?
    I split data into an array to send through the server for greater performance. But I have problems to reassemble them.

    Anyway, Ian's method works in the test.

    Hope this would help me.
    Thanks

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ketting00 View Post
    I split data into an array to send through the server for greater performance. But I have problems to reassemble them.
    Then to help you with future occasions, JSON is the technique that you should be using for the easiest way to transmit and convert the data.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hey Paul,

    You're right.

    How about this array:
    Code:
    var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    for (var i = 0; i < 10; ++i) {
    	console.log(array[i]); // 9
    }
    How do I get it as 0123456789

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ketting00 View Post
    How do I get it as 0123456789
    The join method would be most useful for that, using an empty string for the separator.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •