简体   繁体   中英

scope in javascript.. parent scope variable not retaining value

I am a newbie to javascript..have gone thru multiple scope related blogs and several answers on stackoverflow..but not sure why this code doesnt work..

 function checkPhoneEmail(element, index, array) {
    var _contact = {};
    var _phone_empty = true;
    var _email_empty = true;
    var _phones_to_store = [];
    var _emails_to_store = [];
    var _prev_phone_number;
    var _phone;
    var i;
    //function to check if this phone 
    // should be included
    function checkMobilePhone(ph_element) {
        var _match;


        _match = ph_element.type.match(/mobile/i);
        if (_match && ph_element.value.length >= 10 && !(ph_element.value.match(/^800/) || ph_element.value.match(/^1800/) || ph_element.value.match(/^1-800/))) {
            return true;
        };
        return false;
    };

    if (!_.isEmpty(element.phoneNumbers)) {

        for (i = 0; i < element.phoneNumbers.length; i++) {
            console.log('prev num: ' + _prev_phone_number);
            console.log('curr num: ' + element.phoneNumbers[i].value)
            if (!_.isEqual(_prev_phone_number, element.phoneNumbers[i].value)) {
                if (checkMobilePhone(element.phoneNumbers[i])) {
                    _phone = {
                        id: element.phoneNumbers[i].id,
                        value: element.phoneNumbers[i].value
                    };
                    _phones_to_store.push(_phone);
                    console.log('phone to store: ' + element.phoneNumbers[i].value)
                };
            };
            _prev_phone_number = element.phoneNumbers[i].value;
            console.log('prev1 num: ' + _prev_phone_number);
        };

        _phone_empty = false;
    };

    if (!_.isEmpty(element.emails)) {

    };

};

why is the _prev_phone_number not being set ? I see it at prev1 num..but when you look for the next element its set back to undefined...my understanding is for doesnt create a new scope ? is this incorrect ?

I am trying to remove duplicates from phone contacts array (from cordova contacts) and doing some very basic checks to eliminate all numbers except a valid us mobile # for a mobile app..using above logic if a contact has multiple entries in contacts for same phone number i am seeing duplicates..Tried above logic with foreach and also _.uniq ... but same result..

Any help is appreciated.

sample Data:

{
    "id" : "1916",
    "rawId" : "1911",
    "displayName" : "John Doe",
    "name" : {
        "familyName" : "Doe",
        "formatted" : "John Doe",
        "givenName" : "John"
    },
    "nickname" : null,
    "phoneNumbers" : [{
            "type" : "mobile",
            "value" : "+1 999 666 9175",
            "id" : "11994",
            "pref" : false
        }, {
            "type" : "mobile",
            "value" : "+1 999 666 9175",
            "id" : "12001",
            "pref" : false
        }
    ],
    "emails" : null,
    "addresses" : null,
    "ims" : null,
    "organizations" : null,
    "birthday" : null,
    "note" : "",
    "photos" : null,
    "categories" : null,
    "urls" : null
}

A for loop does not create its own scope. The issue is that you are trying to log to the console an undefined value.

for (i = 0; i < element.phoneNumbers.length; i++) {
        //_prev_phone_number is undefined here.
        console.log('prev num: ' + _prev_phone_number); 
        console.log('curr num: ' + element.phoneNumbers[i].value)
        if (!_.isEqual(_prev_phone_number, element.phoneNumbers[i].value)) {
            if (checkMobilePhone(element.phoneNumbers[i])) {
                _phone = {
                    id: element.phoneNumbers[i].id,
                    value: element.phoneNumbers[i].value
                };
                _phones_to_store.push(_phone);
                console.log('phone to store: ' + element.phoneNumbers[i].value)
            };
        };
        //This is the first time you have defined _prev_phone_number
        _prev_phone_number = element.phoneNumbers[i].value;
        console.log('prev1 num: ' + _prev_phone_number);
    };

You should probably check to see if _prev_phone_number has been defined before logging it to the console.

if(_prev_phone_number){
  console.log('prev num: ' + _prev_phone_number);
}

This would ensure that you are not logging an undefined value.

Instead of using the variable _prev_phone_number , why don't you just access element.phoneNumbers[i-1].value ? No need to introduce a new variable for it.

If that doesn't work, then something is clobbering your object and we will need to look deeper.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM