[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 414
  • Last Modified:

geojson array filter with jquery

 I want to filter the below json array.


condition:  properties.name == "Abandoned Cars"

before  sample.json:
{
   "type":"FeatureCollection",
   "features":[
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               50.8212596,
               6.8961028
            ]
         },
         "properties":{
            "name":"Litter Basket Complaint",
            "description":"",
            "created":"08\/01\/2014 - 14:24",
            "field_status_hex":"cc0000",
            "field_category_hex":"5F9EA0",
            "field_address":"Pingsdorfer Stra\u00dfe 88, 50321 Br\u00fchl",
            "path":"\/spot\/reports\/2b8f-1",
            "field_status_icon":"pause",
            "field_category_icon":"trash",
            "nid":1
         }
      },
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               50.818128126776,
               6.8905774494293
            ]
         },
         "properties":{
            "name":"Graffiti Report",
            "description":"",
            "created":"08\/01\/2014 - 14:24",
            "field_status_hex":"cc0000",
            "field_category_hex":"8B0000",
            "field_address":"Am Ringofen 21, 50321 Br\u00fchl",
            "path":"\/spot\/reports\/d087-2",
            "field_status_icon":"pause",
            "field_category_icon":"graffiti",
            "nid":2
         }
      },
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               50.824350198819,
               6.8955127140167
            ]
         },
         "properties":{
            "name":"Abandoned Cars",
            "description":"",
            "created":"08\/01\/2014 - 14:24",
            "field_status_hex":"5F9EA0",
            "field_category_hex":"00008B",
            "field_address":"Liblarer Stra\u00dfe 88, 50321, Br\u00fchl",
            "path":"\/spot\/reports\/f0b4-3",
            "field_status_icon":"drawer",
            "field_category_icon":"car",
            "nid":3
         }
      },
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               50.828213159666,
               6.9081941982391
            ]
         },
         "properties":{
            "name":"Building Construction Complaint",
            "description":"",
            "created":"08\/01\/2014 - 14:24",
            "field_status_hex":"ff6600",
            "field_category_hex":6400,
            "field_address":"Promenade, 50321 Br\u00fchl",
            "path":"\/spot\/reports\/0dd0-4",
            "field_status_icon":"play",
            "field_category_icon":"office",
            "nid":4
         }
      },
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               50.8327211,
               6.9032226
            ]
         },
         "properties":{
            "name":"Abandoned Cars",
            "description":"",
            "created":"08\/01\/2014 - 14:24",
            "field_status_hex":"5F9EA0",
            "field_category_hex":"00008B",
            "field_address":"Friedrichstra\u00dfe 23 50321 Br\u00fchl",
            "path":"\/spot\/reports\/0a43-5",
            "field_status_icon":"drawer",
            "field_category_icon":"car",
            "nid":5
         }
      },
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               50.826873,
               6.900167
            ]
         },
         "properties":{
            "name":"Litter Basket Complaint",
            "description":"",
            "created":"08\/01\/2014 - 14:24",
            "field_status_hex":"5F9EA0",
            "field_category_hex":"5F9EA0",
            "field_address":"Centre, 50321 Br\u00fchl",
            "path":"\/spot\/reports\/d294-6",
            "field_status_icon":"drawer",
            "field_category_icon":"trash",
            "nid":6
         }
      }
   ]
}

Open in new window


After sample.json:
{
   "type":"FeatureCollection",
   "features":[
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               50.824350198819,
               6.8955127140167
            ]
         },
         "properties":{
            "name":"Abandoned Cars",
            "description":"",
            "created":"08\/01\/2014 - 14:24",
            "field_status_hex":"5F9EA0",
            "field_category_hex":"00008B",
            "field_address":"Liblarer Stra\u00dfe 88, 50321, Br\u00fchl",
            "path":"\/spot\/reports\/f0b4-3",
            "field_status_icon":"drawer",
            "field_category_icon":"car",
            "nid":3
         }
      },
         
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               50.8327211,
               6.9032226
            ]
         },
         "properties":{
            "name":"Abandoned Cars",
            "description":"",
            "created":"08\/01\/2014 - 14:24",
            "field_status_hex":"5F9EA0",
            "field_category_hex":"00008B",
            "field_address":"Friedrichstra\u00dfe 23 50321 Br\u00fchl",
            "path":"\/spot\/reports\/0a43-5",
            "field_status_icon":"drawer",
            "field_category_icon":"car",
            "nid":5
         }
      },
     
   ]
}

Open in new window


How to achieve After sample.json  result using jquery?

my expected code:
 $.getJSON(sample.json).done(function (data) {
$.each(data.features, function (markers, item) {
var returnedData = $.grep(item.properties, function(element, index){
          return element.name == "Abandoned Cars";
    });
// actually i dont how to do after this
document,write(returnedData.description[0]);
docment.write(returnedData.created[0]();
.
.
.
.
.

}
   

Open in new window

0
chrisvee
Asked:
chrisvee
  • 4
  • 4
1 Solution
 
mankowitzCommented:
Try

var new_one = orig.features.filter(function(item) {
   return item.properties.name == "Abandoned Cars"
});

see http://jsfiddle.net/64MF4/
0
 
chrisveeAuthor Commented:
@mankowitz   Great!   I can see the filtered results in html. but can you please  assign the output to a js variable. like (var orig) . so that i can access filtered results from js variable
0
 
mankowitzCommented:
new_one is a json variable.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
chrisveeAuthor Commented:
i mean  if  i          document.write(new_one);      it has to show array format like orig
0
 
chrisveeAuthor Commented:
Sorry. I understood now. I am just a noob in javascript. thanks for the solution
0
 
mankowitzCommented:
ohh.. I see what you are saying.

Try this instead

orig.features = orig.features.filter(function(item) {
   return item.properties.name == "Abandoned Cars"
});

and then you should be able to use orig.features[0].properties.name
0
 
chrisveeAuthor Commented:
Thanks for your answer. please clarify this doubt also.
http://jsfiddle.net/64MF4/

for Orig ,            the array starts with  "type":"FeatureCollection"
for new_one,     the array starts with  "type":"Feature",

But i want    new_one   same as Orig array format.  
Please help!
0
 
mankowitzCommented:
I think I answered this, but you can just do this:

orig.features = orig.features.filter(function(item) {
   return item.properties.name == "Abandoned Cars"
});
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now