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:
-
// remove
-
var arr:Array = new Array("a", "b", "c", "d");
-
arr = arr.remove(1); // pass the index, not value!
-
trace(arr); // a, c, d
-
-
// insert
-
var arr:Array = new Array("a", "c", "d");
-
arr = arr.insert(1, "b");
-
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:
-
Array.prototype.remove = function(index:int):Array {
-
var original:Array = this.slice(); // a, b, c, d
-
var temp:Array = original.splice(index); // b, c, d
-
temp.shift(); // c, d
-
original = original.concat(temp); // a, c, d
-
return original;
-
};
Array.insert(index:int, value:*):
Actionscript:
-
Array.prototype.insert = function(index:int, value:*):Array {
-
var original:Array = this.slice(); // a, c, d
-
var temp:Array = original.splice(index); // c, d
-
original[index] = value; // b
-
original = original.concat(temp); // a, b, c, d
-
return original;
-
};



November 19th, 2009
Marvin Blase
Posted in
Brilliant, exactly what I was looking for, and I managed to convert it to AS2.
Thanks for that awesome posting. It saved MUCH time :-)
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;
};
Right, thanks! Will check that out.
Any update on this ? Excellent information keep it up!
ChessMax, that's a beautiful thing. Thank you.
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;
};
Great comments, thank you all!
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);
}
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.
i Know some thing from this Blog which is Prototype.Such a Great Functionality which is not Provide action-script framework.
Well Awesome Post.
If you are going for most excellent contents like myself, only visit this
site every day for the reason that it presents quality contents, thanks