SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    InArray function

    Hi all i have a problem with a script. I need to check if a value exist into an array, is this value is new i add it to the array otherwise i return false from a
    function.
    function copia()
    {
    cap1 = document.getElementById('cap_da_assegnare');
    //eseguo un ciclo per trovare gli elementi selezionati
    for (i = 0; i < cap1.length; i++) {
    if (cap1.options[i].selected) {
    //recupero id e valore letterale
    v = cap1.options[i].value;
    t = cap1.options[i].text;
    //recupero l'id della select di destinazione
    if (addItem(v)) {
    cap2 = document.getElementById('cap_assegnati');
    len = cap2.length;
    cap2[len] = new Option(t, v, false);
    }
    }
    }

    aggiornaCap();
    }


    The function addItem(v) return true if the value is a new element otherwise false.
    function addItem(val)
    {
    duplicated = false;

    for (i = 0; i < myVector.length; i++) {
    if (myVector[i] == val) {
    duplicated = true;
    }
    }

    if (duplicated == true) {
    return false;
    } else {
    myVector[myVector.length] = val;
    return true;
    }
    }

    The script work as well but when i delete from the myVector element
    and later i try to put new element the browser crash.

    This is the function delItem
    function delItem(val)
    {
    for (i = 0; i < myVector.length; i++) {
    if (myVector[i] == val) {
    myVector.splice(i, 1);
    }
    }
    }

    I'm not an expert on js i hope that my english is enough clearly to understand
    my problem.
    Thanks in advance.

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are changing the length of the array while you are looping through it.

    I like to add methods to all arrays for adding an item only if it does not exist,and removing all instances of the same item from an array.
    They both use another method, indexAt, that returns the array index of a member. Change the names to your own preference...
    Code:
    Array.prototype.addOnce=function(wot){
    	if(this.indexAt(wot)==-1)this.push(wot);
    }
    Array.prototype.indexAt=function(wot){
    	var L=this.length;
    	var i=0;
    	while(i< L){
    		if(this[i]===wot)return i;
    		++i;
    	}
    	return -1;
    }
    Array.prototype.remove=function(wot){
    	var ax;
    	while((ax=this.indexAt(wot))!= -1)this.splice(ax,1);
    }

  3. #3
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    276
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not use indexOf?
    "Never imagine yourself not to be otherwise than what
    it might appear to others that what you were or might
    have been was not otherwise than what you had been
    would have appeared to them to be otherwise."

  4. #4
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Call it whatever you like- but indexOf is a defined array method already in some implementations, with slightly different usage than my method.


Bookmarks

Posting Permissions

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