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

277
Views
Get number of element in array (JS)

I have AJAX call

Here is code

 $.ajax({
        url: '@Url.Action("EmailsList", "Questions")',
        contentType: 'application/json; charset=utf-8',
        type: 'GET',
        dataType: 'json',
        processData: false,
        success: function (result) {
            var email = result;
           // console.log(result[0].Name);
            for (var i = 0; i <= email.length - 1; i++) {
                var emailHTML = '<div style="margin-left: 25px; margin-top: 10px;>' +
                    '<b style="margin-left: 10px;">' + result.indexOf(this)+
                    result[i].Email +
                    '<b>' +
                    '<b style="margin-left: 20px;">' +
                    result[i].Name +
                    '</b>' +
                    '</div>';
                $(".email_list").append(emailHTML);
            }
        }
    });
}

I need to write element number from 1, I tried this result.indexOf(this), but it not works

How I can do this?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can simply use the variable i with which you are iterating like so:

 var emailHTML = '<div style="margin-left: 25px; margin-top: 10px;>' +
                    '<b style="margin-left: 10px;">' +(i+1)+
                    result[i].Email +
                    '<b>' +
                    '<b style="margin-left: 20px;">' +
                    result[i].Name +
                    '</b>' +
                    '</div>';
                $(".email_list").append(emailHTML);

The specific reason for using (i+1) is because i starts its iteration from 0 and you want it from 1.

about 4 years ago · Santiago Trujillo Report

0

If I understood, you should write something like :

var emailHTML = '<div style="margin-left: 25px; margin-top: 10px;>' +
                    '<b style="margin-left: 10px;">' + (i + 1) +
                    result[i].Email +
                    '<b>' +
                    '<b style="margin-left: 20px;">' +
                    result[i].Name +
                    '</b>' +
                    '</div>';

Just use the value of your variable i incremented in loop.

about 4 years ago · Santiago Trujillo 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!