[英]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.我想在 JSON 文件中获取我的谷歌联系人的完整列表,但我只获取我的联系人的最后一个值。
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.费德里科。
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.费德里科。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.