From: Matthew A. S. <ms...@go...> - 2001-03-12 22:12:08
|
To avoid using loop searches like this I have used the idea of lookup tables to help with locating something that I am looking for. I would have basically two arrays: lookup["a"] = 0 lookup["b"] = 1 lookup["c"] = 2 objectA.name = "a"; object[0] = objectA; objectB.name = "b"; object[1] = objectB; objectC.name = "c"; object[2] = objectC; so if I wand to find objectB I look for it by a given name. I just use "b" here. to get objectB's index I just return: i = lookup["b"]; if I actually just want the object then I do: o = object[lookup["b"]]; I also store the name with the object itself so that it can internally determine it's position if needed. I am not certain that I have explained this well. I am also not certain this is more efficient than looping through the array. For all I know that's what JS does when you use arrays with named indexes... But this seems to work really well for me. Any input would be appreciated though. M. -----Original Message----- From: dyn...@li... [mailto:dyn...@li...]On Behalf Of Doug Melvin Sent: Monday, March 12, 2001 4:57 PM To: dynapi-help Subject: [Dynapi-Help] (no subject) is there a better way to determine an objects position in an array other than the below code? for (var i=0;i < tempNode.parentNode.nodes.length;i++){ if (tempNode.parentNode.nodes[i]===tempNode){ index=i; i=tempNode.parentNode.nodes.length; }; }; --- Outgoing mail is certified Virus Free by AVG Free Edition Download at: http://www.grisoft.com/html/us_index.cfm Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.237 / Virus Database: 115 - Release Date: 3/7/01 |