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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.