Kris
Kris

Reputation: 97

Unique values multidimensional array

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
        }));
    }
});

Upvotes: 0

Views: 5640

Answers (1)

CyberNinja
CyberNinja

Reputation: 864

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

Upvotes: 2

Related Questions