简体   繁体   English

我想替换数组中对象的值?

[英]I want to replace the value of an object inside an array?

I have a timestamp in my array that I have removed the UTC letters from and I want to replace the old timestamp with the "new" timestamp(without UTC) Maybe there is an even easier way to do the removing?我的数组中有一个时间戳,我已经从中删除了 UTC 字母,我想用“新”时间戳替换旧时间戳(没有 UTC) 也许有更简单的方法来删除?

So I've tried to loop over my data with .forEach and .map trying to replace it but still haven't figure it out how to exactly do so.所以我试图用 .forEach 和 .map 循环我的数据试图替换它,但仍然没有弄清楚如何准确地这样做。 I've watched a bunch of Stackoverflow threads about this but haven't found a solution that I get to work....clearly missing something or writing something wrong.我看过一堆关于这个的 Stackoverflow 线程,但还没有找到我开始工作的解决方案......显然遗漏了一些东西或写错了一些东西。

So can anyone guide me how to solve this in the best way?那么谁能指导我如何以最佳方式解决这个问题?

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()

Your code looks fine, just refactor that fragment (best approach to work with forEach ):您的代码看起来不错,只需重构该片段(使用forEach最佳方法):

data.forEach((item, index) => {
   const timeStamp = item.timestamp;
   const newTimeStamp = timeStamp.slice(0, 19);
   item.timestamp = newTimeStamp; 
});

and it should work.它应该工作。

Did you know that variables declared with "const" could not be changed?你知道用“const”声明的变量不能改变吗? So it seems like you want to use "var" here.所以看起来你想在这里使用“var”。 The last 3 letters can be removed by "slice(0, -3)".最后 3 个字母可以通过“slice(0, -3)”删除。

var 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
var grabTimeStamp = () => {
  data.forEach(function (object, index) {
   
    var newTimeStamp = object.timestamp.slice(0, -3);
    console.log("newTimeStamp:", newTimeStamp, index);

    //next step to replace the old timestamp with newTimeStamp
    object.timestamp = newTimeStamp;
  });
};
grabTimeStamp();

Since it does seem like you are fairly new to coding, I tried to change only a few things in your code.由于您似乎对编码还很陌生,因此我尝试仅更改您代码中的一些内容。 However your function grabTimeStamp can be done shorter:但是你的函数grabTimeStamp可以做得更短:

function removeTimestamp(data){
    data.foreach((item, index) => {
        item.timestamp = item.timestamp.slice(0, -3);
    });
}
removeTimestamp(data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM