2016-09-22 75 views
2

好吧,我有一個doozie,所以任何幫助非常感謝。我之前在此發佈過這個問題,但可能會遺漏一些背景信息,所以我會再試一次!我有四個geojson圖層,每個圖層都是數組格式。要訪問這些圖層中的信息,它看起來像這樣嵌套:layers-object-features-properties。下面是數據結構的一個樣本:小冊子地圖:按年篩選

{ 
"type": "Feature", 
"properties": { 
"info_city": "Wuerzburg", 
"info_date": 2009, 
"info_status": "fac" 
}, 

我想,以濾除屬性是日期,這在我的數據是場「info_date」。我寫了下面的函數,隨機抽取一年作爲測試過濾。這將被鏈接到我的地圖上的範圍滑塊欄,範圍從1971年至2016年。

function filterByYear(data){ 
    console.log(data) 
    f = data.filter(function(d){ return d.features.properties.info_date === '2016';}) 
    console.log(f) 
    return f; 
    } 

順便說一句,我也嘗試過這種使用underscore.js,不得要領:

f=_.filter(data, function(d) { return d.properties.info_date == 2016; }); 

因此,我調用這個函數在那裏我索引範圍滑尺,像這樣,使用圖層geoJsonLayers.fellows作爲輸入。

if (index == 2015) { 
    filterByYear(geoJsonLayers.fellows) 
    } 

什麼也沒有發生,而在filterByYear功能,我能夠CONSOLE.LOG(數據),但沒有控制檯的(F)。我在這裏做錯了什麼?並且,是否有一種更容易的方法可逐年過濾,因爲我真正想要做的是在用戶移動範圍滑塊時進行過濾,即當index = 1980時,只顯示其中顯示「info_date」== 1980的數據。任何幫助在這裏非常感謝!謝謝。

回答

0

您的問題可能是使用「嚴格平等」您的篩選器檢查。在您的數據結構中,info_date整數,但您嚴格檢查字符串'2016'

嘗試使用==而不是===並查看是否可以解決您的問題。您也可以嘗試檢查2016而不是'2016'


編輯:如果你想解決您的動態檢查一年的問題,你可以通過一年分爲filterByYear作爲參數:

function filterByYear(data, year) { 
    f = data.filter(function(d) { 
    return d.features.properties.info_date == year; 
    }); 
    return f; 
} 

編輯2:以下作品我使用(大約)您的樣本數據。

k = [{ 
    "type": "Feature", 
    "properties": { 
    "info_city": "Wuerzburg", 
    "info_date": 2009, 
    "info_status": "fac" 
    } 
},{ 
    "type": "Feature", 
    "properties": { 
    "info_city": "Berlin", 
    "info_date": 2016, 
    "info_status": "fac" 
    } 
}]; 

function filterByYear(data, year) { 
    f = data.filter(function(d) { 
    return d.properties.info_date == year; 
    }); 
    return f; 
} 


filterByYear(k, 2009); // Returns array of one object 
filterByYear(k, 2016); // Returns array of one object 
filterByYear(k, 2008); // Returns empty array 
+0

Thanks Will!但仍然沒有運氣。我嘗試了你的建議。我認爲數據可能以一種奇怪的方式嵌套,並且無法訪問數據。我不確定我會如何顯示。 – DiamondJoe12

+0

你在瀏覽器中工作嗎?在'filterByYear()'的開頭,你可以通過'console.log(data)'來查看對象的結構,並找出'info_date'是如何嵌套的。 –

+0

請問 - 謝謝,我試過這樣做,我認爲我對結構有更好的處理,但我仍然無法控制'filterByYear()'函數中的任何內容(在每年篩選內)。例如,當我嘗試console.log'd'或'f'時,看看發生了什麼,沒有顯示。我不確定這是爲什麼。因此,這似乎是它可能破裂的地方。 – DiamondJoe12