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

107
Views
Array only showing last result of the loop - JAVASCRIPT

i want to get the full list of my google contacts on a JSON file, but im only getting the last value on my contacts.

What should i change? or what is the problem here?

function contactsJSON() {

  var contactsjson = {}
  var myContacts = ContactsApp.getContactGroup('System Group: My Contacts').getContacts();
  for (var i = 0; i < myContacts.length; i++) {

    var id = myContacts[i].getId();
    var name = myContacts[i].getFullName();

    var emailsobj = myContacts[i].getEmails();
    var email = {}
    for (var j = 0; j < emailsobj.length; j++) {
      email[emailsobj[j].getAddress().toLowerCase()] = 1;
    };

    var phonesobj = myContacts[i].getPhones();
    var phone = {}
    for (var j = 0; j < phonesobj.length; j++) {
      phone[phonesobj[j].getPhoneNumber().replace(/[_)(\s.-]/g, '')] = 1;

    };

    for (var j = 0; j < id.length; j++) {
      contactsjson = { id: id, name: name, phone: phone, email: email };
    }
  }

  contactsjson = JSON.stringify(contactsjson);
  contactsjson = JSON.parse(contactsjson);
  Logger.log(contactsjson);
  return contactsjson;
}

Hope you can help me,

Federico.

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

0

I just figured it out.

The problem was in:

for (var j = 0; j < id.length; j++) {
  contactsjson = { id: id, name: name, phone: phone, email: email };

I changed it to:

 for (var j = 0; j < phonesobj.length; j++) {
  contactsjson[phonesobj[j].getPhoneNumber()] = {id: id, name: name, phone: phone, email: email}

and now its properly looping trough all the elements in My contacts.

Regards,

Federico.

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!