Home > front end >  How to delete Object if value of nested object property value < epoch
How to delete Object if value of nested object property value < epoch

Time:02-12

I have an object tree that contains the following values.

data = {
  TD2n9thHQgyvWdPhOkHQ: {
    heartbeat: 1644637961658,
    joinTime: 1644636756368,
  },
  IASJDAIODJiklk243oi: {
    heartbeat: 1644637961658,
    joinTime: 1644637961658,
  }
 }

I am trying to check if the value of each heartbeat is 10 seconds less than the current time from epoch and if it is, delete the parent object.

Any help would be much appreciated!

CodePudding user response:

data = {
  TD2n9thHQgyvWdPhOkHQ: {
    heartbeat: 1644637961658,
    joinTime: 1644636756368,
  },
  IASJDAIODJiklk243oi: {
    heartbeat: 1644637961658,
    joinTime: 1644637961658,
  }
 }

for(let x in data) {
  // you have each object 
  console.log(data[x])
  // each heartbeat
  console.log(data[x]['heartbeat']);
  // each of joining time
  console.log(data[x]['joinTime']);
  // you can delete object after your condition satisfy  
  delete data[x];
};

CodePudding user response:

You can use the delete operator to completely remove the object that you want.

data = {
  TD2n9thHQgyvWdPhOkHQ: {
    heartbeat: 1644637961658,
    joinTime: 1644636756368,
  },
  IASJDAIODJiklk243oi: {
    heartbeat: 1644637961658,
    joinTime: 1644637961658,
  }
 }

delete data.IASJDAIODJiklk243oi

console.log(data)

this way you can remove object So, apply your conditions and use delete operator

  • Related