| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| I have a collection of objects that may have duplicates in it. What I want to do is to iterate through the list and only add the non duplicates to an array, so as I am adding to the array, I need some way of checking against the current contents of the array. Is this possible, or is there a better way to do this? *** Sent via Developersdex http://www.developersdex.com *** |
|
#2
| |||
| |||
| you could use a dictionary and the 'exists' method The following examples illustrate the use of the Exists method. [JScript] function keyExists(k) { var fso, s = ""; d = new ActiveXObject("Scripting.Dictionary"); d.Add("a", "Athens"); d.Add("b", "Belgrade"); d.Add("c", "Cairo"); if (d.Exists(k)) s += "Specified key exists."; else s += "Specified key doesn't exist."; return(s); }[VBScript] Function KeyExistsDemo Dim d, msg ' Create some variables. Set d = CreateObject("Scripting.Dictionary") d.Add "a", "Athens" ' Add some keys and items. d.Add "b", "Belgrade" d.Add "c", "Cairo" If d.Exists("c") Then msg = "Specified key exists." Else msg = "Specified key doesn't exist." End If KeyExistsDemo = msg End Function |
|
#3
| |||
| |||
| Mike P wrote: > I have a collection of objects that may have duplicates in it. What I > want to do is to iterate through the list and only add the non > duplicates to an array, so as I am adding to the array, I need some > way of checking against the current contents of the array. Is this > possible, or is there a better way to do this? > > > There's no shortcut. You need to do a nested loop. start loop through collection within the collection loop, loop through the array checking to see if object already exists. if not, add the object to the array and move to the next object Do you need more detail than that? if so, we sill need more details. -- Microsoft MVP - ASP/ASP.NET Please reply to the newsgroup. This email account is my spam trap so I don't check it very often. If you must reply off-line, then remove the "NO SPAM" |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.