javascript - How to get these json nested values using angular? -
i have json response:
{ "tags": [ { "name": "solardata", "results": [ { "groups": [ { "name": "type", "type": "number" } ], "attributes": { "customer": [ "acme" ], "host": [ "server1" ] }, "values": [ [ 1429950000000, 46, 3 ], [ 1429960000000, 62, 3 ], [ 1429970000000, 183, 3 ], [ 1429980000000, 156, 3 ], [ 1429990000000, 205, 3 ] ] } ], "stats": { "rawcount": 5 } } ] }
and want able first 2 items of every value part of item. foe example want return [[1429950000000,46],[1429960000000,62],[1429970000000,183],.....] in scope variable can use graph. new angular , web dev in general way i've tried far.
$http({ url: 'file.json', method: 'post', data: '(query data here)' }).then(function(data, status){ $scope.solardata = data.tags.results.values; conosle.log($scope.solardata); });
you can use map:
var custom = data.tags[0].results[0].values.map(function(values) { return [values[0], values[1]]; });
you can use slice if want return lot of items or variable number of them
return values.slice(0, 2); //---------------------^ replace
var data = { "tags": [{ "name": "solardata", "results": [{ "groups": [{ "name": "type", "type": "number" }], "attributes": { "customer": [ "acme" ], "host": [ "server1" ] }, "values": [ [ 1429950000000, 46, 3 ], [ 1429960000000, 62, 3 ], [ 1429970000000, 183, 3 ], [ 1429980000000, 156, 3 ], [ 1429990000000, 205, 3 ] ] }], "stats": { "rawcount": 5 } }] } var custom = data.tags[0].results[0].values.map(function(values) { return [values[0], values[1]]; }); console.log(custom);
Comments
Post a Comment