我有一個<SELECT multiple="multiple">
框中的選項。如何檢查在jQuery.inArray()中是否在另一個數組中找到了一個字符串元素數組?
我將所選選項存儲在JS變量 - selectedFeatures
。
我想通過selectedFeatures
選項的數組變量jQuery.inArray()
- 但函數的目的只是爲了檢查一個值。
我可以顯示/隱藏(或過濾器)在我的地圖標記,如果我確實指定一個元素,像這樣的索引:selectedFeatures[0]
內
..但因爲我可以從選擇框中選擇多個選項這種過濾方法不起作用。
我如何檢查是否另一個數組內發現JS/jQuery的在項目的數組?
// store selected options as an array of string elements inside the variable
var selectedFeatures = $("#features").val();
// for every element inside 'markers.houses' array...
$(markers.houses).each(function(index, elem){
// check if any of the values inside the 'selectedFeatures' array are found
// also inside a sub-array 'features' inside every element of 'markers.houses'
// array. if 'true' show that particular marker, otherwise hide the marker
if(jQuery.inArray(selectedFeatures, elem.features) !== -1 || jQuery.inArray(selectedFeatures, elem.features) > -1){
markers.houseMarkers[index].setVisible(true);
}else{
markers.houseMarkers[index].setVisible(false);
}
});
圖1.1 - 店選擇選項數組變量與
jQuery.inArray()
嗯。但是如何返回第二個數組中找不到的元素解決我的問題?假設我在第一個數組中有5個元素中有3個沒有在第二個數組中找到。所以'array_diff'返回3個元素,我將如何使用它來解決我的問題? – Bob
由於這三個元素不在第二個數組中,這意味着第一個數組中有額外的東西,不應該在那裏讓測試通過。 –
但其他兩個元素匹配,因此我應該顯示至少匹配一個元素的標記。假設用戶從我的功能選擇框中選擇了「游泳池」和「BBQ」。因此,用戶希望看到所有具有游泳池和/或燒烤的標記(房屋)。因此,如果某個標記在其特徵數組中沒有「游泳池」和「BBQ」,請將其隱藏,但如果標記至少匹配「BBQ」,請在地圖上顯示。那麼我得到的非匹配元素的返回點是什麼?我必須錯過一些非常明顯的東西...... – Bob