Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20

Thread: Use of arrays?

  1. #16
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by felgall View Post
    Also why write three or four statements when one is all you need.

    Object.keys(new String(Array(11))).map(Number);

    A single statement written in plain JavaScript.
    The argument given to the Array constructor should be 12 in the above code to create an array equivalent to [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. I'm not certain why though; I've never seen an array passed to the String constructor.

    While it's an interesting technique, I think a regular array is a bit more intuitive. In this case, I would also expect shorter code and better performance from an ordinary array.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  2. #17
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,299
    Thanks
    10
    Thanked 585 Times in 566 Posts
    Quote Originally Posted by Arbitrator View Post
    The argument given to the Array constructor should be 12 in the above code to create an array equivalent to [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. I'm not certain why though; I've never seen an array passed to the String constructor.
    .
    it's simply making an object with the number of numerical keys needed, then turning the object into an array from those keys.

    there's lots of alternatives ways to do this using different built-ins to make the object you need:

    Code:
    Object.keys(new String(Array(11))) ;
    Object.keys(Object(1+Array(10)));
    Object.keys(" ".repeat(10).split(""));
    if you keep an [].map/filter toolkit, and you should, you'll probably be able to do this quite handily in your projects.

    Code:
    function k(a,b){ return a; }
    function b(a,b){ return b; }
    
    " ".repeat(10).split("").map(b)

    and of course now in firefox and hopefully everywhere soon:

    Code:
     [].map.call(" ".repeat(10), (x,y)=>y )
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  3. #18
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by rnd me View Post
    it's simply making an object with the number of numerical keys needed, then turning the object into an array from those keys.
    Yeah, but I'm at a loss for why the value should be 12 instead of 11. If 11 is used, Firefox 27 outputs the array [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] which is a value short even though the length of an eleven-member array (containing the numbers zero through ten) should be 11.

    Quote Originally Posted by rnd me View Post
    there's lots of alternatives ways to do this using different built-ins to make the object you need:

    Code:
    Object.keys(new String(Array(11))) ;
    Object.keys(Object(1+Array(10)));
    Object.keys(" ".repeat(10).split(""));
    if you keep an [].map/filter toolkit, and you should, you'll probably be able to do this quite handily in your projects.

    Code:
    function k(a,b){ return a; }
    function b(a,b){ return b; }
    
    " ".repeat(10).split("").map(b)

    and of course now in firefox and hopefully everywhere soon:

    Code:
     [].map.call(" ".repeat(10), (x,y)=>y )
    How do we un-innovate? This is giving me a headache.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  4. #19
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,299
    Thanks
    10
    Thanked 585 Times in 566 Posts
    Quote Originally Posted by Arbitrator View Post
    Yeah, but I'm at a loss for why the value should be 12 instead of 11. If 11 is used, Firefox 27 outputs the array [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] which is a value short even though the length of an eleven-member array (containing the numbers zero through ten) should be 11.
    the elements are blank and not counted, it's the separators (,) that populate the String object.

    at it's simplest:

    Code:
    alert(Array(3));
    alert(String(Array(3)).length);
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  5. Users who have thanked rnd me for this post:

    Arbitrator (02-17-2014)

  6. #20
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by rnd me View Post
    the elements are blank and not counted, it's the separators (,) that populate the String object.
    Ah, I understand now. When you convert a string to an object, every character in the string becomes a property value associated with a property name in the form of a numeric key. So these three lines are equivalent:

    Code:
    console.log(Object.keys(new String(Array(12))).map(Number));
    console.log(Object.keys(new String(",,,,,,,,,,,")).map(Number));
    console.log(Object.keys(Object(",,,,,,,,,,,")).map(Number));
    Definitely unintuitive.
    For every complex problem, there is an answer that is clear, simple, and wrong.


 
Page 2 of 2 FirstFirst 12

Posting Permissions

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