Remove duplicates from an array
A function that removes duplicates in an array.
by csornmo
HTML
<button>Apple</button>
<button>Banana</button>
<button>Strawberry</button>
<button>Lime</button>
JavaScript
function unique(list) {
var result = [];
$.each(list, function(i, e) {
if ($.inArray(e, result) == -1) {
result.push(e);
if (result.length > 3) {
result.pop();
}
}
});
}
var thelist = [];
$('button').click(function() {
var fruit = $(this).text();
thelist.push(fruit);
console.log( unique(thelist) );
});