1

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.

Community
  • 1
  • 1
Craig
  • 55
  • 1
  • 9
  • For the jQuery function: what version of jQuery are you using? jQ 2+ only supports IE9+ as per docs: http://jquery.com/browser-support/ – Hless Jan 03 '14 at 09:03
  • Also can you confirm if this does or doesn't work in IE8? Built a quick fiddle and can't check 'cause I'm on a Mac: http://jsfiddle.net/9x2xt/1/ – Hless Jan 03 '14 at 09:13
  • Hi I am using 1.7.2 of jQuery. – Craig Jan 03 '14 at 10:08
  • jsfiddle doesn't even work with IE8 lots of javascript errors!!! – Craig Jan 03 '14 at 10:13
  • Oh, haha. I totally forgot about that. oh well seems you have found a solution anyways :) – Hless Jan 03 '14 at 10:27

1 Answers1

0

I noticed that your initial attempt was it native/vanilla JavaScript. I know some frown on sugaring built-ins but I also needed to sort it.

    if(!Array.prototype.unique) {
        Array.prototype.unique = function () {

            var nArr = [],
            oLen = this.length,
            dup, x, y;

            for (x = 0; x < oLen; x++ ){
                dup = undefined;

                for (y = 0; y < nArr.length; y++ ){
                    if( this[x] === nArr[y] ){
                        dup = 1;
                        break;
                    }
                }

                if(!dup) { nArr.push(this[x] ); }
            }
            return nArr;
        };
    }


    var myArr = ["above", "code", "works", "code", "works", "just", "and", "hence"];
    var uniqueArr = myArr.unique(); //["above", "code", "works", "just", "and", "hence"]
scottshane
  • 36
  • 3