rss twitter

Array Prototypes: insert(index,value) & remove(index)

I've been searching the net for a couple of minutes but didn't find a working prototype in AS3 / 2 which fit my requirements. There were lots of prototypes but I wanted to remove an Array-key by just passing it's index. And I needed the other way around: inserting new keys to where-ever I want. Seemed like I'm the only man on earth ever needed this as none of the prototypes I found do so.

Example:

Actionscript:
  1. // remove
  2. var arr:Array = new Array("a", "b", "c", "d");
  3. arr = arr.remove(1); // pass the index, not value!
  4. trace(arr); // a, c, d
  5.  
  6. // insert
  7. var arr:Array = new Array("a", "c", "d");
  8. arr = arr.insert(1, "b");
  9. trace(arr); // a, b, c, d

So I roughly made up my mind how to split, pop/shift and concat the Arrays to achieve the wanted. Didn't take too long and for sure I'd like to share it with you.

Array.remove(index:int):

Actionscript:
  1. Array.prototype.remove = function(index:int):Array {
  2.     var original:Array = this.slice(); // a, b, c, d
  3.     var temp:Array = original.splice(index); // b, c, d
  4.     temp.shift(); // c, d
  5.     original = original.concat(temp); // a, c, d
  6.     return original;
  7. };

Array.insert(index:int, value:*):

Actionscript:
  1. Array.prototype.insert = function(index:int, value:*):Array {
  2.     var original:Array = this.slice(); // a, c, d
  3.     var temp:Array = original.splice(index); // c, d
  4.     original[index] = value; // b
  5.     original = original.concat(temp); // a, b, c, d
  6.     return original;
  7. };

10 Responses to “Array Prototypes: insert(index,value) & remove(index)”

  1. Andrew says:

    Brilliant, exactly what I was looking for, and I managed to convert it to AS2.

  2. chapter 6 says:

    Thanks for that awesome posting. It saved MUCH time :-)

  3. ChessMax says:

    try this (shorter and no array duplicated in the memory):

    Array.prototype.remove = function(index:int):Array
    {
    this.splice(index, 1);
    return this;
    };

    Array.prototype.insert = function(index:int, value:*):Array
    {
    this.splice(index, 0, value);
    return this;
    };

  4. Marvin Blase says:

    Right, thanks! Will check that out.

  5. Any update on this ? Excellent information keep it up!

  6. Chris Merry says:

    ChessMax, that's a beautiful thing. Thank you.

  7. Chris Merry says:

    An extension of ChessMax's work, this prototype allows you to either move an element around inside an array or insert at the specified position

    Array.prototype.move = function(index:int, value:*):Array
    {

    var index_of:uint = this.indexOf(value)

    this.splice(index_of, 1);

    this.splice(index, 0, value);
    return this;

    };

  8. Jeff says:

    Great comments, thank you all!

  9. Chris Merry says:

    I found that using prototypes like this can interfere with the smooth running of a Tweener stack, which I've got around by using them as ordinary functions:

    function remove_from_a1(a1,index):void{

    a1.splice(index,1);

    trace(a1);

    }

    function insert_into_a1(a1,index,value:*):void{

    a1.splice(index,0,value);
    trace(a1);

    }

    function move_within_a1(a1,index,value):void{

    var index_of:uint = a1.indexOf(value)

    a1.splice(index_of, 1);

    a1.splice(index, 0, value);

    trace(a1);

    }

  10. I am usually to blogging and i really respect your content. The article has actually peaks my interest. I am going to bookmark your web site and maintain checking for brand new information.

Leave a Reply

Powered by WordPress | Free T-Mobile Phones for Sale | Thanks to Palm Pre Blog, Video Game Music and Get Six Pack Abs