Frage

I wanted to convert node div elements to array then if there the same arrays I wanted to get rid of them. Where is actually am I making mistake? Thank you

jQuery

var div = $('div').get();
var arr = $.makeArray(div);
var rev = $.unique(arr);
$(rev).appendTo('body');

HTML

<div>1</div>
<div>1</div>
<div>2</div>
<div>3</div>
<div>3</div>
<div>3</div>

DEMO

War es hilfreich?

Lösung

Your mistake is that you're assuming that $.unique compares the elements by their contents.

Whereas it just removes the exactly same nodes from the selection.

It could happen when you, say, use .add() to concatenate one selection set with another.

UPD: the easiest solution I could think of is http://jsfiddle.net/6qe4c/2/

var div = $('div');

var values = [];
div.each(function() {
    var $i = $(this),
        value = $i.text();
    if (values.indexOf(value) != -1) {
        $i.remove();
        return;
    }

    values.push(value);
});
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top