【发布时间】:2021-01-22 10:29:43
【问题描述】:
我的数组中有一个时间戳,我从中删除了 UTC 字母,我想用“新”时间戳替换旧时间戳(没有 UTC) 也许有更简单的方法来进行删除?
所以我尝试用 .forEach 和 .map 循环遍历我的数据,试图替换它,但仍然没有弄清楚如何准确地这样做。 我已经看过一堆关于此的 Stackoverflow 线程,但还没有找到我可以开始工作的解决方案....显然遗漏了一些东西或写错了一些东西。
那么谁能指导我如何以最好的方式解决这个问题?
const data = [
{
timestamp: "2019-03-01 09:00:00UTC",
url: "/blub.html",
userid: "12345"
},
{
timestamp: "2019-03-01 09:00:00UTC",
url: "/cont.html ",
userid: "12346"
},
{
timestamp: "2019-03-01 10:00:00UTC ",
url: "/cont.html ",
userid: "12345"
},
{
timestamp: "2019-03-01 10:30:00UTC",
url: "/ho.html ",
userid: "12347"
}
];
console.log("data", data);
console.log("ex: first data object:", data[0]);
//loop through and grab the timestamp in each object and remove the UTC stamp
const GrabTimeStamp = () => {
data.forEach(function (objects, index) {
const timeStamp = objects.timestamp;
const newTimeStamp = timeStamp.slice(0, 19);
console.log("newTimeStamp:", newTimeStamp, index);
//next step to replace the old timestamp with newTimeStamp
});
};
GrabTimeStamp()
【问题讨论】:
-
嘿,你的想法看起来不错。你知道拼接吗?另外,也许您可以在第一次询问时间戳时进行切片,省去再次循环的麻烦。最后别忘了在函数结束时返回。
-
谢谢@billybadas,是的,首先尝试了.splice,但也没有让它起作用,还有很多练习要做。感谢您的意见!
标签: javascript arrays object replace array.prototype.map