I've got an array of objects:
var arr = [
{
timemark: "2017-03-01",
db_total: c1,
db_used: d1,
hosts: e1,
items: f1
},{
timemark: "2017-03-02",
db_total: c2,
db_used: d2,
hosts: e2,
items: f2
},{
timemark: "2017-03-03",
db_total: c3,
db_used: d3,
hosts: e3,
items: f3
},..]
I am struglling how to transform it to another array but with different structure:
var result = [
{
topic: "db_total",
data: [
{
x: "2017-03-01",
y: c1
},{
x: "2017-03-02",
y: c2
},{
x: "2017-03-03",
y: c3
},...]
},{
topic: "db_used",
data: [
{
x: "2017-03-01",
y: d1
},{
x: "2017-03-02",
y: d2
},{
x: "2017-03-03",
y: d3
},...]
},{
topic: "hosts",
data: [
{
x: "2017-03-01",
y: e1
},{
x: "2017-03-02",
y: e2
},{
x: "2017-03-03",
y: e3
},...]
},{
topic: "items",
data: [
{
x: "2017-03-01",
y: f1
},{
x: "2017-03-02",
y: f2
},{
x: "2017-03-03",
y: f3
},...]
},...];
I know I have to do something like this:
//convert
var result = [];
for (var i=0; i<arr.length; i++) {
result[i]=[arr[i].timemark];
}
Which creates array of arrays:
[
[2017-03-01],
[2017-03-02],
[2017-03-03]
]
It is kinda a start after some hours. But I can't find a way how to start creating objects inside the array instead of arrays? Trying to go baby steps :)
but I am really having problems understanding the snippet and probably using wrong syntax can't get it to work.
Could someone explain how to properly use loop in this case?