I have this array:
[{"temp":"24.44","time":"2021-11-26 22:23:29.370657"},
{"temp":"25.44","time":"2021-11-26 22:23:35.411530"}]
And I need this:
data.addRows([
[24.44, [22, 23, 29]],
[25.44, [22, 23, 35]]
]);
It is to be able to make a graph.
1) You can make use of map and split to achieve the desired result.
const arr = [
{ temp: "24.44", time: "2021-11-26 22:23:29.370657" },
{ temp: "25.44", time: "2021-11-26 22:23:35.411530" },
];
const result = arr.map((o) => [
+o.temp,
o.time.split(" ")[1].split(".")[0].split(":").map(Number),
]);
console.log(result);
2) You can also use regex here [^\s]+([^\.]+)/:
const arr = [
{ temp: "24.44", time: "2021-11-26 22:23:29.370657" },
{ temp: "25.44", time: "2021-11-26 22:23:35.411530" },
];
const result = arr.map(({ temp, time }) => {
const [a, b, c] = time.match(/[^\s]+([^\.]+)/)[1].split(":");
return [+temp, [+a, +b, +c]];
});
console.log(result);