![](/img/trans.png)
[英]How to use FILTER on a multidimensional array to retrieve unique values?
[英]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)
祝好運
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.