所以我使用Azure Application Insights REST API从我的Web应用程序获取数据,将其构建成一个图表,以显示在仪表板上。
回来的json如下:
{
"value": {
"start": "2017-08-07T23:01:50.847Z",
"end": "2017-08-08T11:01:50.847Z",
"interval": "PT1H",
"segments": [
{
"start": "2017-08-07T23:01:50.847Z",
"end": "2017-08-08T00:00:00.000Z",
"requests/count": {
"sum": 317
}
},
{
"start": "2017-08-08T00:00:00.000Z",
"end": "2017-08-08T01:00:00.000Z",
"requests/count": {
"sum": 332
}
},
{
"start": "2017-08-08T01:00:00.000Z",
"end": "2017-08-08T02:00:00.000Z",
"requests/count": {
"sum": 337
}
},
{
"start": "2017-08-08T02:00:00.000Z",
"end": "2017-08-08T03:00:00.000Z",
"requests/count": {
"sum": 326
}
}
]
}
}
我需要在“细分”中获取“end”和“sum”值来构建一个类似的数组,因为图表api要求数组采用以下格式:
[
["2017-08-08T00:00:00.000Z", 317],
["2017-08-08T01:00:00.000Z", 332],
["2017-08-08T02:00:00.000Z", 337],
["2017-08-08T03:00:00.000Z", 326]
我怎样才能实现这个目标?
答案 0 :(得分:1)
如果你喜欢某些现代JavaScript ,你也可以这样做:
// “res” is your API response
const newArray = res.value.segments.reduce((acc, val) => {
return [...acc, [val.end, val['requests/count'].sum]];
}, []);
详细了解reduce magic。
我希望它会有所帮助!
答案 1 :(得分:0)
var myArray = resultFromAPI.value.segments.map(function(segment) {
return [segment.end, segment["requests/count"].sum];
});
应该做的伎俩
答案 2 :(得分:0)
使用javascript map。类似的东西:
t.value.segments.map(function(x) { return [ x.start, x['requests/count'].sum] })