简体   繁体   中英

Filtering json data array with javascript

I have a json data like below

 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 ] 
     }
   }
]

I want to filter the json data above to get properties and coordinates based on time, I have tried using code below, but it's not showing the data I want

  var data_filter=data.filter(function(item){
    return item.properties.time==1;         
});
console.log(data_filter)

Do you know how I'm filtering the json data to get properties and coordinate data ?

Thank you

You could finally map the wanted properties.

 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; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM