javascript - how to create an array of objects from an $.each loop in jquery -
hie, trying create geojson object array of objects. using $.each()
iterate through array , trying return array of object named kendofeatures (a geojson object) this:
kendofeatures.type = "feature"; kendofeatures.properties = { "id": _id, "id": _id, "faulttype": _faulttype, "entryguid": _entryguid, "date": _datcaptured, "status": _status, "area": _area, "zone": _zones, "latitude": _latutude, "longitude": _longitude, "comments": _comments, "user": _fullname, "userid": _idnumber, "userphone": _phonenumber, "usermail": _email }; kendofeatures.geometry = { "type": "point", "coordinates": [ _latutude, _longitude ] };
the code using looks this:
function displayfilterresults() { var kendofeatures = {}; var datasource = $("#grid").data("kendogrid").datasource; console.log(datasource); // todo:gets filter datasource var filters = datasource.filter(); console.log(filters); // todo:gets full set of data data source var alldata = datasource.data(); console.log(alldata); // todo:applies filter data var query = new kendo.data.query(alldata); console.log(query); var filtereddata = query.filter(filters).data; console.log(filtereddata); var geojsonobject = { "type": "featurecollection", "features": [] }; $(filtereddata).each(function (index, item) { var _area = item.area; var _email = item.c_email; var _fullname = item.c_fullname; var _idnumber = item.c_idnumber; var _phonenumber = item.c_mobilenumber; var _comments = item.comments; var _datcaptured = item.datetimecaptured; var _faulttype = item.faulttype; var _id = item.id; var _entryguid = item.recordid; var _reportedvia = item.reportedvia; var _status = item.responce; var _latutude = item.xcoordinate; var _longitude = item.ycoordinate; var _zones = item.zone; kendofeatures.type = "feature"; kendofeatures.properties = { "id": _id, "id": _id, "faulttype": _faulttype, "entryguid": _entryguid, "date": _datcaptured, "status": _status, "area": _area, "zone": _zones, "latitude": _latutude, "longitude": _longitude, "comments": _comments, "user": _fullname, "userid": _idnumber, "userphone": _phonenumber, "usermail": _email }; kendofeatures.geometry = { "type": "point", "coordinates": [ _latutude, _longitude ] }; console.log(kendofeatures); geojsonobject.features.push(kendofeatures); }); console.log(geojsonobject);
}
instead of array distinct objects properties in filtereddata
, featurecollection
resulting in array of same object same properties, geometry etc. question how return desired featurecollection
. geojsonobject
can like:
{ "type": "featurecollection", "features": [ { "type": "feature", "properties": { "id": 1, "id": 1, "faulttype": "sewer burst", "entryguid": "0ectey-yu78ee7e-01", "date": "2016/04/01", "status": "assigned", "area": "senga", "zone": "residential", "geometry": { "type": "point", "coordinates": [ 24.960937499999996, 11.178401873711785 ] } }, { "type": "feature", "properties": {}, "geometry": { "type": "point", "coordinates": [ 30.585937499999996, -19.642587534013032 ] } }, { "type": "feature", "properties": { "id": 2, "id": 2, "faulttype": "water burst", "entryguid": "0ectey-yu78yre7e-01", "date": "2016/02/01", "status": "closed", "area": "cbd", "zone": "cbd", }, "geometry": { "type": "point", "coordinates": [ 24.43359375, -9.535748998133627 ] } }, { "type": "feature", "properties": { "id": 3, "id": 3, "faulttype": "pot hole", "entryguid": "07yye54y-yu78ee7e-01", "date": "2016/03/21", "status": "unassigned", "area": "riverside", "zone": "residential", }, "geometry": { "type": "point", "coordinates": [ 45.439453125, -17.476432197195518 ] } } ] }
filtereddata
looks this:
[ { area: "senga" c_email: "tkmawarire@live.com" c_fullname: "takudzwa kain mawarire" c_idnumber: "29-279485 b 27" c_mobilenumber: "+263779960053" comments: "main hole burst near far end" datetimecaptured: fri apr 15 2016 01: 14: 40 gmt + 0200(south africa standard time) faulttype: "sewer burst" id: 1 recordid: "a63c8656-437c-4d7a-9fe3-dad5edf73e08" reportedvia: "web application" responce: "assigned" xcoordinate: 29.8395538 ycoordinate: -19.51572 zone: "institution" }, { area: "senga" c_email: "tkmawarire@live.com" c_fullname: "takudzwa kain mawarire" c_idnumber: "29-279485 b 27" c_mobilenumber: "+263779960053" comments: "sewer burst near football pitch" datetimecaptured: fri apr 15 2016 23: 58: 40 gmt + 0200(south africa standard time) faulttype: "sewer burst" id: 2 recordid: "c776808c-3f09-4221-99e6-11a44bcec692" reportedvia: "web application" responce: "unassigned" xcoordinate: 29.8392315 ycoordinate: -19.5195084 zone: "institution" }, { area: "senga" c_email: "tkmawarire@live.com" c_fullname: "takudzwa kain mawarire" c_idnumber: "29-279485 b 27" c_mobilenumber: "+263779960053" comments: "water bleading ground" datetimecaptured: sat apr 16 2016 01: 18: 08 gmt + 0200(south africa standard time) faulttype: "water pipe burst" id: 7 recordid: "aff0ece0-b084-4031-8cc8-43ca66f6adb5" reportedvia: "web application" responce: "closed" xcoordinate: 29.8388252 ycoordinate: -19.5002327 zone: "institution" } ]
move featurecollection = [];
outside of each loop. want start blank array before loop , push object onto array during iteration.
var featurecollection = []; $(filtereddata).each(function(index, item) { var kendofeatures = {}; // ... featurecollection.push(kendofeatures); }); var geojsonobject = { "type": "featurecollection", "features": featurecollection };
or, define geojsonobject
first , use in loop directly:
var geojsonobject = { "type": "featurecollection", "features": [] }; $(filtereddata).each(function(index, item) { var kendofeatures = {}; // ... geojsonobject.features.push(kendofeatures); });
Comments
Post a Comment