0

I have made a multidimensional array and want to filter duplicate values. I tried several solutions which i have found on the web but they don't work well. see my code below how i'm making my array.

while (listItemEnumerator.moveNext()) {
    item = listItemEnumerator.get_current();

    //Get Aanvrager and add to array
    if (item.get_item("Aanvrager")) {
        aanvragerslijst.push({
            id: item.get_item("ID"),
            value: item.get_item("Aanvrager")
        });
    }

    //&& jQuery.inArray(item.get_item("Afdelingshoofd"), afdelingshoofdlijst) == 0)


    //Get Afdelingshoofd and add to array
    if (item.get_item("Afdelingshoofd")) {
        afdelingshoofdlijst.push({
            id: item.get_item("ID"),
            value: item.get_item("Afdelingshoofd").get_lookupValue()
        });
    }     
}


$.each(afdelingshoofdlijst, function (key, value) {
    if (value) {
        $('#LKAfdelingshoofd').append($("<option/>", {
            value: value.value,
            text: value.value
        }));
    }
});
Kris
  • 97
  • 3
  • 15
  • 1
    It would be better if you can simplify the problem to the bare minimal, abstract it from your code, give us the input and expected output and a demo to reproduce the issue. – elclanrs Jun 02 '14 at 07:09
  • possible duplicate of [How to get only unique items in a multidimensional array with JavaScript/jquery?](http://stackoverflow.com/questions/20355058/how-to-get-only-unique-items-in-a-multidimensional-array-with-javascript-jquery) – Karlen Kishmiryan Jun 02 '14 at 11:13

1 Answers1

2

Try the following

function getUniqueValues(array, key) {
var result = new Set();
array.forEach(function(item) {
    if (item.hasOwnProperty(key)) {
        result.add(item[key]);
    }
});
return result;
}

Usage:

var uniqueArr = getUniqueValues(yourArr,keyYouWant)

Reference:Get Unique Values in a MultiDim Array

Good Luck

Arpit Vasani
  • 1,013
  • 8
  • 19
CyberNinja
  • 872
  • 9
  • 25