push multivalue elements to array in titanium (JS)

475 Views Asked by At

In titanium im accessing the phone book and i have an array with contact information (single and multi-value fields). i have troubles with the multi-value emails field. the multi-value fields stringified objects looks like this (work, home, other as well as multiple emails for work, home, etc):

email: {"work":["[email protected]","www.icloud.com"]}   
phone: {"work":["(555) 766-4823"],"other":["(707) 555-1854"]}

the code i have so far:

function buddy_check(){
    var all_emails= []; // array of emails from senders contacts
    var multiValue = ['email'];
    var people = Ti.Contacts.getAllPeople(); // gets all contacts (recordId, name,…)
    for (var i=0, ilen=people.length; i<ilen; i++){
        Ti.API.info('---------------------');
        var person = people[i];
    //for (var j=0, jlen=singleValue.length; j<jlen; j++){
    //  Ti.API.info(singleValue[j] + ': ' + person[singleValue[j]]);
    // }
    for (var j=0, jlen=multiValue.length; j<jlen; j++){
        Ti.API.info(multiValue[j] + ': ' + JSON.stringify(person[multiValue[j]]));
        all_emails.push();
    }
}

i need all emails of the phone book in one array, separated by comma. underscore functions would work as well.

what do i have to push to the all_emails array? is there a simpler way to extract the emails and put it in an array (e.g search for "@")?

thx for sharing insights!

P.S: the user is of course informed that the emails are being checked with our database.

1

There are 1 best solutions below

0
On

from here: http://www.oodlestechnologies.com/blogs/How-to-extract-contact-list-having-phone-numbers-and-emails-from-iPhone-contacts-using-Titanium

var data = [];
var people = Ti.Contacts.getAllPeople();

for (var i = 0,
    ilen = people.length; i < ilen; i++) {
    var person = people[i];
    var title = people[i].fullName;
    if (!title || title.length === 0) {
        title = "No Name";
    }
    Ti.API.info("person name : " + title);

    var personEmails = [];
    //this check is used for conforming that array will contain at least one email that is actual.
    var actualConfirmed = false;
    //fetching emails
    //Ti.API.info("person email::::1 " + JSON.stringify(person.email));
    for (var temp in person.email) {
        var temp_emails = person.email[temp];
        if (temp_emails && (temp_emails.length > 0)) {
            //Ti.API.info("person email::::2 " + JSON.stringify(temp_emails));
            for (var k = 0; k < temp_emails.length; k++) {
                var temp_email = temp_emails[k];
                var isActualEmail = emailValidation(temp_email);
                Ti.API.info("temp_email  " + temp_email + " :::: isActualEmail " + isActualEmail);
                if (isActualEmail) {
                    actualConfirmed = true;
                    personEmails.push(temp_email);
                }
            }
        }
    }

- See more at: http://www.oodlestechnologies.com/blogs/How-to-extract-contact-list-having-phone-numbers-and-emails-from-iPhone-contacts-using-Titanium#sthash.q7TJF8Lg.dpuf