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

113
Views
Matriz JSON, conteo de javascript

Tengo una matriz JSON en un archivo local llamado data.json, tiene el siguiente aspecto:

 [ { "id":"1", "priority": "P1", "start_date": "2021-01-01 14:40:33" },{ "id":"2", "priority": "P2", "start_date": "2019-01-01 14:40:33" },{ "id":"3", "priority": "P3", "start_date": "2020-01-01 14:40:33" },{ "id":"4", "priority": "P2", "start_date": "2020-01-01 14:40:33" },{ "id":"5", "priority": "P1", "start_date": "2021-01-01 14:40:33" } ]

Estoy tratando de obtener el recuento total establecido por dos condiciones como se ve a continuación, sin embargo, el formato en start_date YYYY-MM-DD HH-MM-SS es estático y no se puede cambiar, quiero poder buscar el solo los primeros 4 dígitos (el año) pero aún no puedo, ¿algún problema?

 import data from "./data.json" assert{ type: "json" }; var count = 0; for (var i = 0; i < data.length; i++) { if ((data[i].priority === 'P1') && (data[i].start_date === "2021")) { count++; } } console.log(count)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

¿Qué tal si en su lugar usamos String.prototype.startsWith :

 import data from "./data.json"; let count = 0; for (let i = 0; i < data.length; i++) { if (data[i].priority === "P1" && data[i].start_date.startsWith("2021")) { count++; } } console.log(count);

También podría intentar usar Array.prototype.filter :

 const count = data.filter( item => item.priority === "P1" && item.start_date.startsWith("2021") ).length;

O con desestructuración:

 const count = data.filter( ({ priority, start_date }) => priority === "P1" && start_date.startsWith("2021") ).length;
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!