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

169
Views
Eliminar parte del contenido json

Quiero que mi archivo json se edite cuando la hora/fecha dada sea del pasado. Intenté eliminar terminar [0] pero esto no elimina la parte del archivo.

Mi archivo json

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

Mi código

 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

Tienes un ciclo infinito, porque nunca actualizas la date en el ciclo.

También debe verificar la condición antes de eliminar, no después.

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

En lugar de eliminar un elemento a la vez, busque el índice del primer elemento que desee conservar y, a continuación, elimine todos los elementos anteriores con 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!