|
-
Nov 7th, 2002, 11:16 PM
#1
Thread Starter
Lively Member
How to Prevent duplicate records in Array?
I'm creating an array as follows. Is there any way to eliminat duplicate records from being entered into the array. I'm using the distinct in the sql statement but the way the tables are laid out I get duplicate names.
function FillEngMakeArray()
{
sql = "Select Distinct Make from EngMake order by Make";
cn = Server.CreateObject("ADODB.Connection");
rs = Server.CreateObject("ADODB.RecordSet");
cn.ConnectionString = "Driver=SQL Server;uid=sa;pwd=;Server=Server;Database=DB";
cn.Open;
rs.Open(sql, cn, 3, 3);
i = 0;
EngMake = new Array(rs.RecordCount);
if (!rs.EOF)
{
do
{
EngMake[i] = rs.Fields("Make").Value;
i = i + 1;
rs.MoveNext();
}
while(!rs.EOF);
return EngMake;
cn.Close;
cn = null;
}
else
{
EngMake = ("No Record Found");
return EngMake;
rs.close;
rs = null;
cn.Close;
cn = null;
}
}
-
Nov 8th, 2002, 04:09 AM
#2
Create a Collection and add the elements of your array to it.
when a duplicate key is entered this will throw an error, trap that
error and continue with the next element in the array.
This is really only a work around your better off trying to get the
SQL statement to work the way you want it to
ps ignore that bit about collections I wasn't thinking straight, your using js I was thinking in VB.
so create a second array
add elements to it but before adding a new element from the
first array loop thru the second to see if it is there allready
inefficient but it will work
Last edited by DeadEyes; Nov 8th, 2002 at 09:18 AM.
-
Nov 8th, 2002, 11:58 AM
#3
Thread Starter
Lively Member
Thanks for the help. I'll work on that angle. I was trying to do it all in the first run.
Thanks again.
-
Nov 8th, 2002, 12:12 PM
#4
Thread Starter
Lively Member
I'm new to JavaScript. I pieced the above code toghther from searching forums and Google. Would you mind giving me a little short example of how to nest that other array?
-
Nov 8th, 2002, 12:36 PM
#5
quick hack at removing duplicates
this is far from perfect ( cos it's half five on a friday)
Code:
<script>
function test(){
var firstarray = [1,5,3,1,1,4];
var secondarray = new Array(firstarray.length); // same length in case no duplicates
k=0;
//loop through the first array
for(i=0;i<firstarray.length;i++){
//loop through the second array
for(j=0;j<secondarray.length;j++){
//if a value in the first matches one in the second
//then duplicate so stop looping thru the second array
if (firstarray[i]==secondarray[j])
break; //breaks out of the inside for loop
//reached the last element of the second array and not found a match
//so this is a new number
if (j==secondarray.length-1){
secondarray[k] = firstarray[i];
k++; //every time you add a new one increment this
}
}
}
//BS just for display
var str="";
for(i=0;i<secondarray.length;i++)
str+= secondarray[i] + ",";
alert(str);
}
test();
</script>
-
Nov 8th, 2002, 04:23 PM
#6
Thread Starter
Lively Member
Thanks a million DeadEyes. I can give it a go from here. Have a good weekend.
Thanks again.
robert
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|