2

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

2
  • Is your data already an object? Commented Sep 11, 2019 at 6:58
  • Hi @Grabatui, yes Commented Sep 11, 2019 at 7:12

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, Thank you for your correction, it's working fine right now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.