On 27 mai, 12:22, Jay Tee <[EMAIL PROTECTED]> wrote:
> On May 26, 8:29 pm, kangax <[EMAIL PROTECTED]> wrote:
>
>
>
> > On May 26, 12:56 pm, Jay Tee <[EMAIL PROTECTED]> wrote:
>
> > > function array_fill(len,val) {
> > >   return (function() {
> > >     var retarray = [];
> > >     $R(0,len,true).each(function(elem) {
> > >       retarray[elem] = val;
> > >     });
> > >     return retarray;
> > >   }());
>
> > > }
>

Or perhaps this function may help you, it includes more use cases and
allows array resizing without using push(). Test case included (the
element 'debug' is a DIV) :

      function testDebug(a, t, c) {
        $('debug').insert( '<code>[' + a.toString() + '] ' + t + '...
' + (c ? 'Pass' : 'Failed') + '</code><br />'  );
      }

      /**
       * array.fill(value)
       * array.fill(value, length)
       * array.fill(value, start, length)
       *
       * NOTE : start and length are not range checked
       */
      Array.prototype.fill = function(value,start,length) {
        if (isNaN(length)) {
          length = start || this.length;
          start = 0;
        }
        var n=start || 0;
        var len=(n+length);
        if (len>=this.length) this.concat(new Array(len-this.length));
        while (n<len) {this[n++]=value;}
        return this;
      };

      var arr1;

      arr1 = new Array();
      $('debug').insert('<p>new Array();</p>');

      testDebug(arr1, 'length = 0', arr1.length == 0 );
      testDebug(arr1, 'toString() = ""', arr1.toString() == '' );

      arr1 = new Array().fill(0);
      $('debug').insert('<p>new Array().fill(0);</p>');

      testDebug(arr1, 'length = 0', arr1.length == 0 );
      testDebug(arr1, 'toString() = ""', arr1.toString() == '' );

      arr1 = new Array(3);
      $('debug').insert('<p>new Array(3);</p>');

      testDebug(arr1, 'length = 3', arr1.length == 3 );
      testDebug(arr1, 'toString() = ",,"', arr1.toString() == ',,' );

      arr1 = new Array().fill(0, 3);
      $('debug').insert('<p>new Array().fill(0, 3);</p>');

      testDebug(arr1, 'length = 3', arr1.length == 3 );
      testDebug(arr1, 'toString() = "0,0,0"', arr1.toString() ==
'0,0,0' );

      arr1.fill(1, 4);
      $('debug').insert('<p>fill(1, 4);</p>');

      testDebug(arr1, 'length = 4', arr1.length == 4 );
      testDebug(arr1, 'toString() = "1,1,1,1"', arr1.toString() ==
'1,1,1,1' );

      arr1.fill(2, 2, 2);
      $('debug').insert('<p>fill(2, 2, 2);</p>');

      testDebug(arr1, 'length = 4', arr1.length == 4 );
      testDebug(arr1, 'toString() = "1,1,2,2"', arr1.toString() ==
'1,1,2,2' );

      arr1.fill(3, 5, 2);
      $('debug').insert('<p>fill(3, 5, 2);</p>');

      testDebug(arr1, 'length = 7', arr1.length == 7 );
      testDebug(arr1, 'toString() = "1,1,2,2,,3,3"', arr1.toString()
== '1,1,2,2,,3,3' );

      arr1.fill(4, 1, 3);
      $('debug').insert('<p>fill(4, 1, 3);</p>');

      testDebug(arr1, 'length = 7', arr1.length == 7 );
      testDebug(arr1, 'toString() = "1,4,4,4,,3,3"', arr1.toString()
== '1,4,4,4,,3,3' );


Hope this helps. (And if the core folks think this is core worthy, I
think it could make a pretty addon.)

yanick
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to