Here is object that i get from api
{
status: true,
code: 200,
msg: "Successfully",
response: {
1609927200: {
o: "1.2338",
h: "1.2344",
l: "1.2333",
c: "1.23395",
v: "5436",
t: 1609927200,
tm: "2021-01-06 10:00:00",
},
1609930800: {
o: "1.2338",
h: "1.23495",
l: "1.2333",
c: "1.2337",
v: "5333",
t: 1609930800,
tm: "2021-01-06 11:00:00",
},
1609934400: {
o: "1.23375",
h: "1.23495",
l: "1.233",
c: "1.234",
v: "5636",
t: 1609934400,
tm: "2021-01-06 12:00:00",
},
}
}
And I want turn it to object wtith arrays like this
{
ohlcv: [
[1609927200, 1.2338, 1.2344, 1.2333, 1.23395, 5436],
[1609930800, 1.2338, 1.23495, 1.2333, 1.2337, 5333],
[1609934400, 1.23375, 1.23495, 1.233, 1.234, 5636],
],
}
With a lot of effort, i ended up with this result with folowing code:
*this.symbol hold the object for the example*
var res = Object.keys(this.symbol).map((item) => {
return {
ohlvc: Object.keys(this.symbol[item]).map((key) => {
return Object.values(this.symbol[item][key]);
}),
};
})[3];
{
"ohlvc": [
[ "1.2338", "1.2344", "1.2333", "1.23395", "5436", 1609927200, "2021-01-06 10:00:00" ],
[ "1.2338", "1.23495", "1.2333", "1.2337", "5333", 1609930800, "2021-01-06 11:00:00" ],
[ "1.23375", "1.23495", "1.233", "1.234", "5636", 1609934400, "2021-01-06 12:00:00" ],
]
}
but:
2.and the element in the 5th position which is timestamp should be on first place.
Any help is appreciated
Here is process method, have the key
(with desired order of chars in sequence).
Use Object.values
, map
and +
operator (convert to int).
const process = (obj, key = "ohlcv") => ({
[key]: Object.values(obj.response).map((item) =>
[new Date(item.tm).getTime()].concat([...key].map((kk) => item[kk]))
),
});
const data = {
status: true,
code: 200,
msg: "Successfully",
response: {
1609927200: {
o: "1.2338",
h: "1.2344",
l: "1.2333",
c: "1.23395",
v: "5436",
t: 1609927200,
tm: "2021-01-06 10:00:00",
},
1609930800: {
o: "1.2338",
h: "1.23495",
l: "1.2333",
c: "1.2337",
v: "5333",
t: 1609930800,
tm: "2021-01-06 11:00:00",
},
1609934400: {
o: "1.23375",
h: "1.23495",
l: "1.233",
c: "1.234",
v: "5636",
t: 1609934400,
tm: "2021-01-06 12:00:00",
},
},
};
console.log(process(data))