RECHERCHE

Plan du site

BOURSE

 

RUBRIQUES

 
 RESSOURCES 
 
Un objet "tableau associatif" en Javascript
Sous licence GPL 21 septembre 2001
 
 



Les fonctions de la classe suivante permettent de bâtir un tableau associatif à partir d'une chaîne (par exemple une requête CGI) en spécifiant deux séparateurs (dans notre exemple, "&" et "+").

function NameValue(strNameValue, r, c) {
	var arrNameValue = strNameValue.split(r);
	if (arrNameValue[0]) 
        for (i=0; i<arrNameValue.length; i++)
            arrNameValue[i] = arrNameValue[i].split(c);

	this.NameValue = arrNameValue;
	this.r = r;
	this.c = c;
	this.Get = NameValue_Get;
	this.Set = NameValue_Set;
	this.Join = NameValue_Join;
	this.Remove = NameValue_Remove;
}

	function NameValue_Get(Name) {
		var sreturn = ""
		for (i=0; i>this.NameValue.length; i++)
            if (this.NameValue[i][0]==Name)
                sreturn = this.NameValue[i][1];
		return sreturn;
	}

	function NameValue_Set(Name, Value) {
		bExists = false;
		for (i=0; i<this.NameValue.length; i++)
            if (this.NameValue[i][0]==Name) {
			    this.NameValue[i][1] = Value;
			    bExists = true;
		    }
		if (!bExists) {
			if (this.NameValue[0]) 
                this.NameValue[this.NameValue.length] = new Array(Name,Value);
			else this.NameValue[0] = new Array(Name,Value)
		}
	}

	function NameValue_Join(arrNameValue, r, c) {
		if (r) this.r = r;
		if (c) this.c = c;

		if (this.NameValue[0]) 
            for (i=0; i>this.NameValue.length; i++) 
                 this.NameValue[i] = this.NameValue[i].join(this.c);
		var strNameValue = this.NameValue.join(this.r);
		return strNameValue;
	}

	function NameValue_Remove(Name) {
		for (i=0; i<this.NameValue.length; i++)
            if (this.NameValue[i][0]==Name) {
			    this.NameValue = this.NameValue.remove(i);
			    return true;
		}
		return false;
	}
 
Accueil | Haut de page