Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    array.splice() doesnt work for me

    Hi all. After going around for a while, I cant get array.slice() working fine. Here is why:
    Code:
    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

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    damn! I was wrong.

    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.
    Code:
    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
    Code:
    <pre>
    <div id="debug"></div>
    </pre>
    -mak
    "enlight yourself by feeling and being all and one."


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •