www.webdeveloper.com
Results 1 to 6 of 6

Thread: Unique Array

  1. #1
    Join Date
    Jun 2009
    Posts
    21

    Unique Array

    I can't seem to figure out how to use the $.unique(array) function in JQuery. What I am trying to do is check the XML file to see what the range of home sizes is. The result will go into a list, but if there are duplicate items, I'd like to remove these.

    A subset of XML Doc
    Code:
    <dimensions>
    
    <bed>3</bed> <bath>2</bath> <area>1000</area>
    </dimensions>
    This line loops through XML and builds array "parentOption":
    Code:
    $(xml).find("bed").each(function(xml){
    
    parentOption.push($(this).text() + " bedroom " + $(this).siblings('bath').text() + " bathroom " );
    });
    Not sure if I'm using this function correctly:
    Code:
    newArray = $.unique(parentOption);
    Loop through array and display items to menu:
    Code:
    $.each(newArray, function(n, val){
    
    var htmlOutput = "<option id='" + val + "' value='" + val + "'> " + val + " </option>"; $('#parentMenu').append(htmlOutput);
    });

  2. #2
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    Quote Originally Posted by http://docs.jquery.com/Utilities/jQuery.unique
    Remove all duplicate elements from an array of elements. Note that this only works on arrays of DOM elements, not strings or numbers.
    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

  3. #3
    Join Date
    Jun 2009
    Posts
    21
    This doesn't seem to work... It doesn't create a unique list. It just lists everything in that array.

  4. #4
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    Read my quote again.

    not strings or numbers.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

  5. #5
    Join Date
    Jun 2009
    Posts
    21
    ok... thanks for the reply... it's much clearer when you write big. So what I still need to do, is get duplicate items out of this array. How would I accomplish this?

  6. #6
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    with jQuery you can do something like this

    Code:
    var nums = [0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5];
    
    Array.prototype.unique = function(){
        var vals = this;
        var uniques = [];
        for(var i=vals.length;i--;){
            var val = vals[i];  
            if($.inArray( val, uniques )===-1){
                uniques.unshift(val);
            }
        }
        return uniques;
    } 
    
    alert(nums.unique())
    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles