SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to remove duplicates?

    Hey,

    I make the following AJAX Request to a page:

    Code:
        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:

    Code:
    $("#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,
    Software Engineer ASP, ASP.NET, VB, PHP

    Kind regards
    Billy

  2. #2
    SitePoint Addict
    Join Date
    Nov 2008
    Location
    Thailand
    Posts
    329
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I did something similar to this recently. This isn't jquery

    Just a start, working just with the names

    Code JavaScript:
    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"]

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,095
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    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
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    SitePoint Addict
    Join Date
    Nov 2008
    Location
    Thailand
    Posts
    329
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

    Code HTML4Strict:
    <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>

  5. #5
    SitePoint Addict
    Join Date
    Nov 2008
    Location
    Thailand
    Posts
    329
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    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
    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:

    Code:
    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?
    Software Engineer ASP, ASP.NET, VB, PHP

    Kind regards
    Billy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •