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

116
Views
JSON array, javascript count

I have a JSON array in a local file called data.json, looks the following:

[
    {
        "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"
    }
]

I'm trying to get total count set by two conditions as seen below, however the formatting on the start_date YYYY-MM-DD HH-MM-SS is static and can't be changed, I want to be able to look for the first 4 digits only (the year) but still unable to, any toughts?

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

What about instead using 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);

You could also try instead using Array.prototype.filter:

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

Or with destructuring:

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!