【问题标题】:How to join several arrays in one?如何将多个数组合二为一?
【发布时间】:2022-01-04 23:00:10
【问题描述】:

我有这个数组:

[{"temp":"24.44","time":"2021-11-26 22:23:29.370657"},
 {"temp":"25.44","time":"2021-11-26 22:23:35.411530"}]

我需要这个:

data.addRows([
    [24.44, [22, 23, 29]], 
    [25.44, [22, 23, 35]]
  ]);

就是能做图。

【问题讨论】:

  • 您要提取.temp参数和.time参数的hourminutesecond?作为[temp, [hour, minute, second]]?您是否在需要此结果的代码中创建了数组?可能有助于包含您为此编写的其他代码。
  • 你能贴出你试过的代码吗?

标签: javascript google-visualization datetime-format


【解决方案1】:

1) 您可以使用mapsplit 来达到预期的效果。

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)你也可以在这里使用正则表达式[^\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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2020-09-07
    • 1970-01-01
    • 2019-01-16
    相关资源
    最近更新 更多