Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

110
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda