如何检测javascript中的值是相同还是不同?

时间:2014-07-28 04:43:31

标签: javascript

请帮助我,如何检测值是否相同?我有一个脚本可以自动为Google地图打开一个新的浏览器标签,并搜索检测到的家庭住址。但是,如果有两个不同的地址怎么办?怎么检测呢?我有两种类型的家庭住址,一种是联系地址,另一种是永久地址。有一段时间,这些类型的地址具有不同的值。如果检测到具有不同值的永久地址,则会为Google地图打开一个新的单独浏览器标签。简而言之,如果两种类型的家庭地址彼此不同,它将打开两个谷歌地图。一个用于联系,一个用于永久性。请帮我。谢谢

这是检测地址的脚本:

 function scanLapVerification() {
    try {
        //check if the page is activity where address check is done
        var page_title = "Title";
        var el = getElement(document, "class", "view-operator-verification-title", "");
        if (!el || el.length == 0) return;
        if (el[0].innerText != page_title) return;
        var page_title = '';
        var el = getElement(document, "class", "workflowActivityDetailPanel", "");
        if (el && el.length > 0) {
            var eltr = getElement(el[0], "tag", "tr", "");
            if (eltr && eltr.length > 0) {
                //Read Contact and Permanent address
                var addresses = {
                    CA: { province: null, municipality: null, barangay: null, zip: null, street: null, snumber: null, building: null, floor: null }
                   ,PA: { province: null, municipality: null, barangay: null, zip: null, street: null, building: null, bnumber: null, floor: null }
                };
                var address_type = null;
                for (var i = 0; i < eltr.length; i++) {
                    tr_text = eltr[i].innerText;
                    if (tr_text.substr(0, "Contact address".length) == "Contact address") address_type = "CA";
                    if (tr_text.substr(0, "Permanent address".length) == "Permanent address") address_type = "PA";
                    if (address_type && tr_text.substr(0, "Province".length) == "Province") {
                        addresses[address_type].province = tr_text.substr("Province".length + 1, tr_text.length - "Province".length - 1);
                    }
                    if (address_type && tr_text.substr(0, "Barangay".length) == "Barangay") {
                        addresses[address_type].barangay = tr_text.substr("Barangay".length + 1, tr_text.length - "Barangay".length - 1);
                    }
                    if (address_type && tr_text.substr(0, "ZIP Code".length) == "ZIP Code") {
                        addresses[address_type].zip = tr_text.substr("ZIP Code".length, tr_text.length - "ZIP Code".length);
                    }
                    if (address_type && tr_text.substr(0, "Street name".length) == "Street name") {
                        addresses[address_type].street = tr_text.substr("Street name".length, tr_text.length - "Street name".length);
                    }
                }
            var contact_address = addresses.CA.street
                                + '' + addresses.CA.barangay 
                                + '' + addresses.CA.province
                                + '' + addresses.CA.zip;
            contact_address = contact_address.replace(/\s+/g, ' ').trim();
            return { content: "address_check", contact_address: contact_address, permanent_address: null };
        }
    }
    return { status: "KO" };
} catch (e) {
        alert("Exception: scanLapVerification\n" + e.Description);
    return { status: "KO", message: e };
}
};

以下是打开新浏览器标签的脚本:

 function scanLapVerification() {
 msgbox("sendRequest: scanLapVerification");
 chrome.tabs.sendRequest(tabLapVerification, { method: "scanLapVerification" },
    function (response) {
        msgbox("receiveResponse: scanLapVerification " + jsonToString(response, "JSON"));
        // maintaining state in the background
        if (response.data.content == "address_check") {
            //open google maps with request for contact address
             var gmaps;
                if (confirm("Do you want to proceed on Google Maps and search for Contact Address?") == true) {
                gmaps = tabCreate("http://maps.google.com/maps?q=" + encodeURIComponent(response.data.contact_address));
            } else {
               gmaps = "You Cancel!";
            }
            document.getElementById("maps").innerText = gmaps;
        }
    }
);
}

1 个答案:

答案 0 :(得分:-1)

您似乎正在尝试对两个对象进行深度相等检查,如果两个对象的所有属性相同,则返回true。如果您可以使用第三方库,则可以尝试使用isEqual function of Underscore.js。这里讨论了一些其他选项:How to determine equality for two JavaScript objects?