P
ROTOTYPE
I'm Not Only The Prototype, I'm Also A Member.
home
▪
stats
▪
search
▪
linkback
▪
about
▪
FAQ
| user: guest,
login
,
register
Array
:
Array.insertAt(index,value)
author:
ceon
[+]
,
Submitted: 12.27.02 4a
• Last Edit: 08.12.03 5a
/* insert a value at selected index of an Array F.Boukhalfa dec 2002. @revised because it was really ugly :) francois@asso-scies.com */ Array.prototype.insertAt = function(index,value) { this.splice(index,0,value); };
usage
[code] toto = new Array("toto","tata","titi"); toto.insertAt(1,"tutu"); trace(toto); [/code] results --> "toto","tutu","tata","titi" hope that helps someone.
msg
1
{
ceon
[+]
, posted: 12.27.02 8a•-, top
[^]
}
nope :)
won't work .
check the debugger :
Array.prototype.insertAt = function(i,e) { this.splice(0,0,this.splice(0,i),e, this.splice(i)); }; toto = new Array(); for(i=0;i<1000;i++) toto[toto.length] = "gffgfg"; toto.insertAt(60,"titi"); trace(toto[0] + "typeof : " + typeof(toto[0]));
splice method seems to return an object .
msg
2
{
senocular
[+]
, posted: 12.27.02 8a•12.27.02 9a, top
[^]
}
silly me, what was I thinking heh
splice does it alone
toto.splice(60,0,"titi")
msg
3
{
ceon
[+]
, posted: 12.27.02 10a•-, top
[^]
}
ooops :)
you're right so ...
it's a bit useless but just for fun :
Array.prototype.insertAt = function(index,value)
{ this.splice(index,0,value);
};
msg
4
{
senocular
[+]
, posted: 12.27.02 12p•-, top
[^]
}
it makes it more intutive :) I mean look at cyberfunk's 'invert a number':
Number.prototype.invert=function(){
return -this;
}
myNumber.invert()
when it would be easier to use
-myNumber
;)
msg
5
{
senocular
[+]
, posted: 09.16.03 5a•-, top
[^]
}
This version [MX] will work on inserting arrays into the array - that is the array values of the array passed and not the array itself as one element
Array.prototype.insertArrayAt = function(index,ary){ this.splice.apply(this, [index,0].concat(ary)); return this; };
Add Comment
[+]
›opyleft 2001-2010. Layer51 is: Jaime Prado.
@