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

184
Views
What is wrong with this counter (if-statement)? (Javascript)

I tried to loop threw an array in order to count the occurance of 'phone1'. The Data from the backend has the following structure:

[{
    Key: 'phone1',
    Recording: {
      ID: 'phone1',
      Tel: '1000',
      User: 'Maria',
      Price: '350',
      docType: 'asset'
    }]

My function for this looks like:

backendData.list[0].map((phone, i) => {
            var counter = 0;
             for (var i = 0; i < backendData.length; i++) {
                if (backendData[i].Key === 'phone1') counter++;
             }
             console.log(counter);
          });

The result I get is always 0, but it has to be 1 with my Data.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Try this:

let counter = 0;
backendData.list[0].forEach((phone, i) => {
   if(phone.Key === 'phone1') counter++;
   console.log(counter);
})
about 4 years ago · Juan Pablo Isaza Report

0

You just can use Array.prototype.filter() to count objects by condition:

const backendData = [{Key:'phone1',Recording:{ID:'phone1',Tel:'1000',User:'Maria',Price:'350',docType:'asset'}}];

const result = backendData
  .filter(({ Key }) => Key === 'phone1')
  .length;

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}

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!