簡體   English   中英

唯一值多維數組

[英]Unique values multidimensional 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
        }));
    }
});

嘗試這個

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)

參考: 在MultiDim數組中獲取唯一值

祝好運

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM