You're here: Snippet Directory » HTML/JavaScript (65)
Language:

JavaScript Name-value Object

Language: English
Programming Language: JavaScript
Published by: pjjt [not registered]
Last Update: 5/15/2006
Views: 362


Description

Hash? Associative array? Dictionary object? Something that's always been missing from JavaScript.

Code

1 function NameValue(strNameValue, r, c) { 2 var arrNameValue = strNameValue.split(r); 3 if (arrNameValue[0]) for (i=0; i<arrNameValue.length; i++) arrNameValue[i] = arrNameValue[i].split(c); 4 5 this.NameValue = arrNameValue; 6 this.r = r; 7 this.c = c; 8 this.Get = NameValue_Get; 9 this.Set = NameValue_Set; 10 this.Join = NameValue_Join; 11 this.Remove = NameValue_Remove; 12 } 13 14 function NameValue_Get(Name) { 15 var sreturn = "" 16 for (i=0; i<this.NameValue.length; i++) if (this.NameValue[i][0]==Name) sreturn = this.NameValue[i][1]; 17 return sreturn; 18 } 19 20 function NameValue_Set(Name, Value) { 21 bExists = false; 22 for (i=0; i<this.NameValue.length; i++) if (this.NameValue[i][0]==Name) { 23 this.NameValue[i][1] = Value; 24 bExists = true; 25 } 26 if (!bExists) { 27 if (this.NameValue[0]) this.NameValue[this.NameValue.length] = new Array(Name,Value); 28 else this.NameValue[0] = new Array(Name,Value) 29 } 30 } 31 32 function NameValue_Join(arrNameValue, r, c) { 33 if (r) this.r = r; 34 if (c) this.c = c; 35 36 if (this.NameValue[0]) for (i=0; i<this.NameValue.length; i++) this.NameValue[i] = this.NameValue[i].join(this.c); 37 var strNameValue = this.NameValue.join(this.r); 38 return strNameValue; 39 } 40 41 function NameValue_Remove(Name) { 42 for (i=0; i<this.NameValue.length; i++) if (this.NameValue[i][0]==Name) { 43 this.NameValue = this.NameValue.remove(i); 44 return true; 45 } 46 return false; 47 } 48

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS