0

I'm trying do do something like this:

   var items = $("main a").unique();
   var links = [];
   items.each(function (index, value)
   {
        links.push({
            href: this.href, 
            text:  this.text
        });
   });

the idea is to find all links on page and remove duplicates, producing an object with href/text pairs. But the above does not work, I'm getting "TypeError: $(...).unique is not a function"

1
  • which jquery version u using? Commented Jan 26, 2018 at 1:30

2 Answers 2

2

You are calling $.unique() incorrectly. It is not a method of the jQuery object, it is an actual function. You really want:

$.uniqueSort($("main a"))

Also, learn to use .map() instead of pushing arrays:

var links = $.uniqueSort($("main a")).map(function(idx, e) {
    return {href: e.href, text: e.text}
})

(Note: this .map() is the jQuery version, where the first argument to the callback is the array/object's index instead of the array element, because $.uniqueSort() applied to a jQuery object returns a jQuery object not a native JavaScript array).

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but using map(), "argument e" just returns array numeric keys, like 0,1 etc, how do I access actual values like href, and text?
got it, it needs second argument to access properties.
ahh apparently uniqueSort on a jQuery object does not return a real javascript array, it returns a jQuery object, and when you map against it, it runs jquery map not javascript map
0

The correct syntax for unique in jQuery is :

$.uniqueSort()    

$.unique() is deprecated as of jQuery 3.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.