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

91 views Asked by At

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?

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. 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.

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

2

There are 2 answers

1
Sa1m0n On BEST ANSWER

Your code looks fine, just refactor that fragment (best approach to work with forEach):

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

and it should work.

4
Passiv Programmer On

Did you know that variables declared with "const" could not be changed? So it seems like you want to use "var" here. The last 3 letters can be removed by "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:

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