I am using the following code in my project :-
uniqueArray = checkboxitems.filter(function(elem, pos, self) {
return self.indexOf(elem) == pos;
})
uniqueArray and checkboxitems are defined as var uniqueArray/checkboxitems = new Array();
checkboxitems array contains a list of text values used to populate checkboxes and contain duplicates. The above code works just fine in Firefox and Google Chrome but due to missing .filter and .indexOf in IE8 it doesn't work in that browser. So having searched on here and found references to mozillas shims such as I tried to use it. But it doesn't seem to work. The res array and hence my uniqueArray variables are never populated.
I also tried this code replacing the a and unique array with mine
var a = [1,5,1,6,4,5,2,5,4,3,1,2,6,6,3,3,2,4];
// note: jQuery's filter params are opposite of javascript's native implementation :(
var unique = $.makeArray($(a).filter(function(i,itm){
// note: 'index', not 'indexOf'
return i == $(a).index(itm);
}));
// unique: [1, 5, 6, 4, 2, 3]
taken from using jquery but again this doesn't work.
Anyone got any ideas how I can get this to work?
Thanks Craig
UPDATE I have now used from here :- jQuery function to get all unique elements from an array?
$.extend({
distinct : function(anArray) {
var result = [];
$.each(anArray, function(i,v){
if ($.inArray(v, result) == -1) result.push(v);
});
return result;
}
});
which works as I wanted.
One thing I did discover however is that the code from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf resulted in the index value being incremented 1 too many! However using the code from here Fixing JavaScript Array functions in Internet Explorer (indexOf, forEach, etc.) worked as expected.