[英]How to get these json nested values using angular?
I have this json response: 我有这个json响应:
{
"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 I want to be able to only get the first two items of every value part of the item. 而且我希望只能获取该项目每个值部分的前两个项目。 Foe example I want to return [[1429950000000,46],[1429960000000,62],[1429970000000,183],.....] in a scope variable so I can eventually use it for a graph. 敌人的例子我想在范围变量中返回[[1429950000000,46],[1429960000000,62],[1429970000000,183],.....],以便最终可以将其用于图形。 I am new to angular and web dev in general but this is the way I've tried it so far. 我一般对angular和Web开发人员是陌生的,但是到目前为止,这是我尝试过的方法。
$http({
url: 'file.json',
method: 'POST',
data: '(query data here)'
}).then(function(data, status){
$scope.solarData = data.tags.results.values;
conosle.log($scope.solarData);
});
var custom = data.tags[0].results[0].values.map(function(values) {
return [values[0], values[1]];
});
You can use slice if you want to return a lot of items or a variable number of them like 如果要返回很多项目或可变数量的项目,可以使用slice
return values.slice(0, 2);
//---------------------^ replace this
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);
var requirementArray = new Array();
for (i = 0; i < $scope.solarData.length ; i++) {
var pare = new Array();
pare.push($scope.solarData[i][0]);
pare.push($scope.solarData[i][1]);
requirementArray.push(pare);
}
requirementArray will be : requireArray将为:
[[1429950000000,46],[1429960000000,62],[1429970000000,183],.....] [[1429950000000,46],[1429960000000,62],[1429970000000,183],.....]
You can use Array.map
for that: 您可以Array.map
使用Array.map
:
$http({
url: 'file.json',
method: 'POST',
data: '(query data here)'
}).then(function(data, status){
$scope.solarData = data.tags[0].results[0].values.map(
function(curVal, index, arr) {
return [curVal[0], curVal[1]];
}
);
conosle.log($scope.solarData);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.