arrays - How to parse a JSON with respect to time stamp -
this controller
var app = angular.module("app", []); app.controller('ctrl',["$scope","$http", function($scope,$http,) { $scope.observations=[]; $scope.totaloutputdata =[]; $scope.tiltedirradiancedata =[]; $scope.time =[]; $scope.demojsonlist = { "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 2, "max_score": 1, "hits": [ { "_index": "solarpowerdemo1", "_type": "solar1", "_id": "1", "_score": 1, "_source": { "observations": [ { "sensor": "weather", "record": [ { "output": [ { "name": "tilted_irradiance", "value": 8, "type": "double", "unit": "w/m2" }, { "name": "horizontal_irradiance", "value": 23, "type": "double", "unit": "w/m2" }, { "name": "ambient_air_temperature", "value": 20, "type": "double", "unit": "°c" }, { "name": "relative_humidity", "value": 35, "type": "double", "unit": "%" }, { "name": "wind_direction", "value": 27, "type": "double", "unit": "degrees" }, { "name": "wind_speed", "value": 27, "type": "double", "unit": "m/s" }, { "name": "precipitation", "value": 27, "type": "double", "unit": "mm/h" }, { "name": "module_temperature", "value": 27, "type": "double", "unit": "°c" } ], "starttime": "2014-07-19t10:37:19.000", "position_global": { "altitude": "10m", "location": { "lat": 22.610927503182157, "lon": 88.45465055391536 } } } ] } ] } }, { "_index": "solarpowerdemo1", "_type": "solar1", "_id": "2", "_score": 1, "_source": { "observations": [ { "sensor": "weather", "record": [ { "output": [ { "name": "tilted_irradiance", "value": 9.32, "type": "double", "unit": "w/m2" }, { "name": "horizontal_irradiance", "value": 23.23, "type": "double", "unit": "w/m2" }, { "name": "ambient_air_temperature", "value": 20.66, "type": "double", "unit": "°c" }, { "name": "relative_humidity", "value": 68, "type": "double", "unit": "%" }, { "name": "wind_direction", "value": 27.88, "type": "double", "unit": "degrees" }, { "name": "wind_speed", "value": 27.22, "type": "double", "unit": "m/s" }, { "name": "precipitation", "value": 27.22, "type": "double", "unit": "mm/h" }, { "name": "module_temperature", "value": 32, "type": "double", "unit": "°c" } ], "starttime": "2015-07-19t11:38:19.000", "position_global": { "altitude": "10m", "location": { "lat": 22.6109257, "lon": 88.4546501536 } } } ] } ] } }, { "_index": "solarpowerdemo1", "_type": "solar1", "_id": "2", "_score": 1, "_source": { "observations": [ { "sensor": "weather", "record": [ { "output": [ { "name": "tilted_irradiance", "value": 19.32, "type": "double", "unit": "w/m2" }, { "name": "horizontal_irradiance", "value": 123.23, "type": "double", "unit": "w/m2" }, { "name": "ambient_air_temperature", "value": 210.66, "type": "double", "unit": "°c" }, { "name": "relative_humidity", "value": 168, "type": "double", "unit": "%" }, { "name": "wind_direction", "value": 217.88, "type": "double", "unit": "degrees" }, { "name": "wind_speed", "value": 227.22, "type": "double", "unit": "m/s" }, { "name": "precipitation", "value": 207.22, "type": "double", "unit": "mm/h" }, { "name": "module_temperature", "value": 312, "type": "double", "unit": "°c" } ], "starttime": "2015-08-19t11:38:19.000", "position_global": { "altitude": "10m", "location": { "lat": 22.6109257, "lon": 88.4546501536 } } } ] } ] } } ] } }; $scope.obslist = function() { $scope.observations = $scope.demojsonlist.hits.hits; console.log($scope.observations.length); for(var j=0;j<$scope.observations.length;j++){ console.log($scope.observations[j]); console.log($scope.observations[j]._source.observations); $scope.records = []; $scope.globalposition =[]; $scope.obs1=[]; $scope.alloutputs =[]; angular.foreach($scope.observations[j]._source.observations, function(obs, index) { angular.foreach(obs.record, function(recrd, index){ $scope.records.push(recrd); }); }); console.log(angular.tojson($scope.records)); console.log($scope.records.length); $scope.alloutputs = angular.copy($scope.records[0].output); $scope.time.push($scope.records[0].starttime); console.log($scope.alloutputs.length); angular.foreach($scope.alloutputs, function(ao, index){ $scope.totaloutputdata.push(ao); }) console.log($scope.totaloutputdata); } console.log($scope.alloutputs); for(var m=0;m<$scope.time.length;m++){ var datobj={ starttime:$scope.time[m], name:[], value:[] } } $scope.value =[]; $scope.name =[]; angular.foreach($scope.totaloutputdata, function(tod, index){ $scope.name.push(tod.name); $scope.value.push(tod.value); }); console.log($scope.value); }; $scope.obslist(); }]);
i want output this:
object {starttime: "2014-07-19t10:37:19.000", name: array[24], value: array[24]} object {starttime: "2015-07-19t11:38:19.000", name: array[24], value: array[24]} object {starttime: "2015-08-19t11:38:19.000", name: array[24], value: array[24]}
but here values getting inserted in name , in values want want first 8 values inserted in first timestamp , next 8 values inserted next timestamp , on.
if care use undersore.js can use chain, or replace functions custom code:
_(_(_.flatten(_.flatten(list.hits.hits.map(function(d) { return d._source.observations; })).map(function(d) { return d.record; })))).map(function(d) { return { starttime: d.starttime, names: _(d.output).map(function(n) { return n.name; }), values: _(d.output).map(function(o) { return o.value; }) } });
explanation: first map , flatten list important parts (hits, , records). can turn list of records desired output format (startime, names, values).
Comments
Post a Comment