[英]Filtering json data array with javascript
我有一個JSON數據如下
var data = [
{
"type": "Feature",
"id": 1,
"properties":
{
"name": "William",
"categorie": 107,
"laporan":"Fire",
"time":1,
"type": "Firefighter",
"phone_number": "0111111"
}, "geometry":
{
"type": "Point",
"coordinates": [ 106.814893, -6.219156]
}
}
,
{
"type": "Feature",
"id": 7,
"properties":
{
"name": "John",
"categorie": 103,
"laporan":"Thief",
"time":3,
"type": "Police",
"phone_number": "0987654321"
}, "geometry":
{
"type": "Point",
"coordinates": [ 106.794107, -6.286935 ]
}
}
]
我想過濾上面的json數據以基於時間獲取properties
和coordinates
,我嘗試使用下面的代碼,但是沒有顯示我想要的數據
var data_filter=data.filter(function(item){
return item.properties.time==1;
});
console.log(data_filter)
您知道我如何過濾json數據以獲取屬性和協調數據嗎?
謝謝
您最終可以映射所需的屬性。
var data = [{ type: "Feature", id: 1, properties: { name: "William", categorie: 107, laporan: "Fire", time: 1, type: "Firefighter", phone_number: "0111111" }, geometry: { type: "Point", coordinates: [106.814893, -6.219156] } }, { type: "Feature", id: 7, properties: { name: "John", categorie: 103, laporan: "Thief", time: 3, type: "Police", phone_number: "0987654321" }, geometry: { type: "Point", coordinates: [106.794107, -6.286935] } }], result = data .filter(item => item.properties.time == 1) .map(({ properties, geometry: { coordinates } }) => ({ properties, coordinates })); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.