Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
Remove part of json content

I want my json-file to be edited when the given time/date is in the past. I've tried delete termine[0] but this does not delete the part of the file.

My json-file

    [{
    "datum": "2020-10-10T10:00:00",
    "event": "..."
    },
    {
    "datum": "2021-10-10T10:00:00",
    "event": "..."
    },...
    ]

My Code

let termine = JSON.parse(fs.readFileSync("/home/pi/NeonBot/termine.json", "utf8"))

var now = new Date();
var date = termine[0].datum
    date = new Date(date)

do {
    delete termine[0]
    console.log(`Deleted Event`)
} while (date < now)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have an infinite loop, because you never update date in the loop.

You should also check the condition before deleting, not after.

while (new Date(termine[0].datum_) < now) {
    delete termine[0];
    console.log("Deleted Event");
}

Instead of deleting one element at a time, find the index of the first element to keep, then remove all the ones before it with splice.

let index = termine.findIndex(event => new Date(event.datum) >= now);
if (index > 0) {
    termine.splice(0, index);
}

fs.writeFileSync("/home/pi/NeonBot/termine.json", JSON.stringify(termine), "utf8");
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!