How to remove duplicates?

Hey,

I make the following AJAX Request to a page:


    function search() {
        $.ajax({ type: "GET",
            url: "ajaxBatchAllocate.asp",
            data: { "SearchStr": $("#SearchStr").val(),
                "AgentID": $("#AgentID").val()
            },
            dataType: "xml",
            success: function (xml) {
                $("#AvailableTourCodes").html("");
                $(xml).find("Tour").each(function () {
                    $("#AvailableTourCodes").append("<option name=\\"TourID\\" value=" + $(this).attr("TourID") + ">" + $(this).text() + "</option>");
                });
            },
            error: function (t) {
                $("#AvailableTourCodes").html("");
            }
        });
    }

I am referring to this line:


$("#AvailableTourCodes").append("<option name=\\"TourID\\" value=" + $(this).attr("TourID") + ">" + $(this).text() + "</option>");
                });

Basically is populates a <select> but some of the <option> have duplicate records, so typically the results may be like so:

<option value=“91978”>1 CHLIB0211*</option>
<option value=“91979”>1 CHLIB0211*</option>
<option value=“91990”>1 CHLIB0211*</option>
<option value=“91983”>1 PEPPM0311*</option>
<option value="91985>1 PEPPM0311*</option>
<option value=“88692”>4RIA1111</option>

What i need to do is remove the duplicates so show he results like this:

<option value=“91978”>1 CHLIB0211*</option>
<option value=“91983”>1 PEPPM0311*</option>
<option value=“88692”>4RIA1111</option>

The value can be ignored, as i know they are not unique. But the main thing i need to do is simply remove the duplicate names to achieve the above.

Is this possible?

Thanks,

I did something similar to this recently. This isn’t jquery

Just a start, working just with the names

var codes = ['1 CHLIB0211*','1 CHLIB0211*','1 PEPPM0311*','1 CHLIB0211*','1 PEPPM0311*','4RIA1111'];

// need to group the same codes together first using sort
codes.sort(); // ["1 CHLIB0211*", "1 CHLIB0211*", "1 CHLIB0211*", "1 PEPPM0311*", "1 PEPPM0311*", "4RIA1111"];

console.log(codes.filter(function(val, i , arr){
      if (val !== arr[i - 1]) return true; // if current index not the same as last store it
    })); // ["1 CHLIB0211*", "1 PEPPM0311*", "4RIA1111"]

Is there a special reason you want to do this using javascript instead of ASP? It seems a bit weird to me to let ASP send too many information and then throw out out stuff you don’t want with javascript. If you filter the duplicates in ASP you get less data traffic and javascript doesn’t have to process anything, making the script faster on two accounts.
And yes, I do realize the ASP script will probably get slightly slower, but I’m pretty sure it’s worth it :slight_smile:

Right I’m not familiar with Jquery, so I’m sure there is a more elegant approach to this.

Jquery’s filter only has an index argument, so you can’t reference the original array/object

In addition as I don’t have your xml I have had to improvise for this demo. I’m sure this can be improved on.

<html>
<head>

</head>
<body>
<ul id = 'testList'>
<li tourId = '91978'>1 CHLIB0211*</li>
<li tourId = '91979'>1 CHLIB0211*</li>
<li tourId = '91980'>1 PEPPM0311*</li>
<li tourId = '91981'>1 CHLIB0211*</li>
<li tourId = '91982'>1 PEPPM0311*</li>
<li tourId = '91983'>4RIA1111</li>
</ul>

<form>
<select id = 'testSelect' name = 'testSelect'></select>
</form>
<body>
<script type = 'text/javascript' src = 'jquery.js'></script>
<script type = 'text/javascript'>
var sorted = $('#testList li').
  sort(function(a, b){ 
  
    if($(a).text() > $(b).text()) return 1; 
  
  });
sorted.
  filter(function(i){
      
   if ( $(sorted[i]).text() !==  $(sorted[i-1]).text() ) return true;

  }).
    each(function () {
      $("#testSelect").
        append("<option name=\\"TourID\\" value=" + $(this).attr("tourID") + ">" + $(this).text() + "</option>");
    });
</script>
</body>
</html> 

Is there a special reason you want to do this using javascript instead of ASP? It seems a bit weird to me to let ASP send too many information and then throw out out stuff you don’t want with javascript. If you filter the duplicates in ASP you get less data traffic and javascript doesn’t have to process anything, making the script faster on two accounts.
And yes, I do realize the ASP script will probably get slightly slower, but I’m pretty sure it’s worth it

Don’t know ASP, but that had crossed my mind.

I see what you mean, it would be even better if this was taken care of in the stored procedure itself.

Now i know this is not an SQL related thread but i will post it anyway, just to show how it would be possible to take care of things like this in the back end rather than client side.

This is my SQL query:


DECLARE @AgentID int = 515, @SearchStr varchar(max) = 'CHLI'

SELECT	DISTINCT	
		Q.ID, 
		Q.Code
FROM	Quote Q
LEFT JOIN Transport T ON Q.ID = T.QuoteID
LEFT JOIN Route R ON T.RouteID = R.ID
INNER JOIN Lookup L ON Q.TID = L.ID
WHERE	Q.Depart >= GETDATE()
AND		Q.SID IN (2500, 2550, 2540)
AND		Q.Code LIKE ISNULL('%' + NULLIF(@SearchStr,'') + '%','%')
AND		Q.ID NOT IN (SELECT QuoteID FROM TourAgent WHERE AgentID = @AgentID)
AND		(L.Name <> 'Pot' OR R.Code = '')
GROUP BY Q.Code, Q.ID, Q.SID
ORDER BY Code

I think i need to somehow check it here, because currently i am trying to do it in the ASP which to be honest i know is not the best option…

What do you guys think? :rolleyes: