...

View Full Version : array.splice() doesnt work for me



stack13
03-22-2005, 10:21 PM
Hi all. After going around for a while, I cant get array.slice() working fine. Here is why:


var holder = new Array ();
holder[0] = new Array ();
holder[0]["ID"] = 1;
holder[0]["TITLE"] = "Revistas";
holder[0]["DESCRIPTION"] = "Revistas Viejas";
holder[0]["PUBLICATION_DAY"] = 1;
holder[0]["PUBLICATION_MONTH"] = 1;
holder[0]["PUBLICATION_YEAR"] = 2005;
holder[0]["EXPIRATION_DAY"] = 1;
holder[0]["EXPIRATION_MONTH"] = 5;
holder[0]["EXPIRATION_YEAR"] = 2006;

holder[1] = new Array ();
holder[1]["ID"] = 2;
holder[1]["TITLE"] = "Mis Fotos";
holder[1]["DESCRIPTION"] = "Imagenes Personales";
holder[1]["PUBLICATION_DAY"] = 22;
holder[1]["PUBLICATION_MONTH"] = 2;
holder[1]["PUBLICATION_YEAR"] = 2005;
holder[1]["EXPIRATION_DAY"] = 30;
holder[1]["EXPIRATION_MONTH"] = 10;
holder[1]["EXPIRATION_YEAR"] = 2006;

holder[2] = new Array ();
holder[2]["ID"] = 3;
holder[2]["TITLE"] = "Descargas";
holder[2]["DESCRIPTION"] = "Libros y otras yerbas";
holder[2]["PUBLICATION_DAY"] = 1;
holder[2]["PUBLICATION_MONTH"] = 1;
holder[2]["PUBLICATION_YEAR"] = 2005;
holder[2]["EXPIRATION_DAY"] = 23;
holder[2]["EXPIRATION_MONTH"] = 3;
holder[2]["EXPIRATION_YEAR"] = 2007;

function removeElement(array,e){
if(!array[e]) return false;
return array.splice(e,1);
}

removeElement(this.holder,e);

If var e is the last key of array, [2 in this case] the function works good. If lower than 2 instead, splice() removes e++. Should I use parseInt() or anything like that? I tried adding -1 but things get even worst. I guess Im using this method quite specifically, maybe an error at defining? Thanks in advance.

-mak

liorean
03-23-2005, 12:49 AM
Hmm. Array.prototype.splice should take the index indicating excision/insertion starting point, the length of the eventual excision, and then a variable number of insertion elements. The index is zero-based, but you seem aware of that already. That means that splice(0,1) should remove the first element ("Revistas"), splice(1,1) the second ("Mis Fotos"), splice(2,1) the third ("Descargas").

And from testing your code, that is just what happens.

stack13
03-23-2005, 01:47 PM
You're right. It was a mistake of mine. I append here some debug tools that may bring some help if you need instant array printing. It supports 2 dimension arrays.


function append(id,text) {
nod=document.createTextNode(text);
var target=document.getElementById(id);
target.innerHTML="";
return target.appendChild(nod);
}
function print_r(id,array) {
var Str="";
Str+=id+"\n";
for(var a in array) {
if(typeof(array[a])=="object") {
Str+="["+a+"] => array\n(\n\t";
for(var e in array[a]) {
Str+="["+e+"] => "+array[a][e]+"\n\t";
}
}
else {
Str+="["+a+"] => "+array[a]+"\n";
}
Str+="\r)\n";
}
return append(id,Str);
}
print_r("debug",array)

and html


<pre>
<div id="debug"></div>
</pre>

-mak
"enlight yourself by feeling and being all and one."



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum