7 Replies - 739 Views - Last Post: 05 August 2014 - 07:07 PM

#1 tenkepadu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 20-July 14

How to do the right code of comparing two objects in pure/native JavaS

Posted 31 July 2014 - 10:39 PM

What is the right code in pure/native Javascript, if the data 1 and data 2 are both different, there will be 2 new tab will open? But if the data 1 and data 2 have same value, there's only 1 new tab will open? I have a script of comparing the values of data 1 and data 2, but I don't know how to do the right and proper code of comparing 2 objects. I read articles and questions in stackoverflow. But still I don't understand how to do that.

Please help me. I'm a newbie in Javascript. Thank you

Here's the code of comparing two object:

function scanLapVerification() {
    try {
        //check if the page is activity where address check is done
        var page_title = "Prelim checks - CB BAP";
        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 = {
                    D1: { province: null, municipality: null, barangay: null, zip: null, street: null, snumber: null }
                   ,D2: { province: null, municipality: null, barangay: null, zip: null, street: null, bnumber: null }
                };
                var address_type = null;
                for (var i = 0; i < eltr.length; i++) {
                    tr_text = eltr[i].innerText;
                    if (tr_text.substr(0, "Data 1".length) == "Data 1") address_type = "D1";
                    if (tr_text.substr(0, "Data 2".length) == "Data 2") address_type = "D2";
                    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);
                    }
                }
            //Comparing of two objects
            var data1 = addresses.D1.street
                                + '' + addresses.D1.barangay 
                                + '' + addresses.D1.province
                                + '' + addresses.D1.zip;
            data1 = data1 .replace(/\s+/g, ' ').trim();

            var permanent_address = data1 ;

            alert(data1 === data2); //true
            alert(data1 != data2); //false

            return { content: "address_check", data1 : data1 , data2: data2};


Here's the code to open new tab/window:

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;
            var popupWindow = null;
            if (confirm("Do you want to proceed on Google Maps and search for Data 1?") == true) {
                //(data1 === data2);
                gmaps = tabCreate("http://maps.google.com/maps?q=" + encodeURIComponent(response.data.data1));
            } else if (confirm("Do you want to proceed on Google Maps and search for Data 2?") == true) {
                //(data1!= data2);
                gmaps = tabCreate("http://maps.google.com/maps?q=" + encodeURIComponent(response.data.data2));
                //Open popup window to input new address
            } else if (confirm("Does the client has new address?") == true) {
                popupWindow = window.open('/NewAddress.htmNewAddress', 'width=700,height=350');
                popupwindow.focus();
            } else {
                gmaps = "";
            }
            document.getElementById("maps").innerText = gmaps;
        }
    }
);
}


Is This A Good Question/Topic? 0
  • +

Replies To: How to do the right code of comparing two objects in pure/native JavaS

#2 ge∅  Icon User is offline

  • D.I.C Addict

Reputation: 80
  • View blog
  • Posts: 559
  • Joined: 21-November 13

Re: How to do the right code of comparing two objects in pure/native JavaS

Posted 01 August 2014 - 01:38 AM

What do your objects look like ?
Was This Post Helpful? 0
  • +
  • -

#3 tenkepadu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 20-July 14

Re: How to do the right code of comparing two objects in pure/native JavaS

Posted 01 August 2014 - 01:45 AM

View Postge∅, on 01 August 2014 - 01:38 AM, said:

What do your objects look like ?


Please see thisPosted Image

The values of Contact Address and Permanent Address, that I need to compare
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,251
  • Joined: 08-June 10

Re: How to do the right code of comparing two objects in pure/native JavaS

Posted 01 August 2014 - 03:47 AM

where do you get those objects from?

PS. .innerText is an IE-proprietary property. the respective standard-compliant property is .textContent
Was This Post Helpful? 0
  • +
  • -

#5 ge∅  Icon User is offline

  • D.I.C Addict

Reputation: 80
  • View blog
  • Posts: 559
  • Joined: 21-November 13

Re: How to do the right code of comparing two objects in pure/native JavaS

Posted 01 August 2014 - 04:00 AM

Ok, I can see that some data1 properties are missing in data2 and vice versa. I would :

- build a third object containing all possible properties (you would find both "Street number" AND "Building number" in this object)

- loop through all these properties

- for each iteration, check if the property exists (and is not empty or null) in data1 and data2,

- compare the value of this property in data1 and data2 : if it doesn't match, immediately stop the iteration and return false, if it does match, continue the iteration

- return true if you reach the end of the loop.
Was This Post Helpful? 0
  • +
  • -

#6 tenkepadu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 20-July 14

Re: How to do the right code of comparing two objects in pure/native JavaS

Posted 03 August 2014 - 07:36 PM

How to do that? Please help me. Sorry I'm not really a programmer :(
Was This Post Helpful? 0
  • +
  • -

#7 ge∅  Icon User is offline

  • D.I.C Addict

Reputation: 80
  • View blog
  • Posts: 559
  • Joined: 21-November 13

Re: How to do the right code of comparing two objects in pure/native JavaS

Posted 04 August 2014 - 06:29 AM

I don't like to do this, but beginners usually struggle with logical operations

function compare(a,b,c){
    for(prop in c){ // we loop through the properties of the model

        if(prop in a && prop in b ){ // if a and b share prop as a property
            if(a[prop] != b[prop]){ // but prop has different values
                return false;
            }
        }
        else{ // if a and b don't share prop as a property
            if(prop in a && a[prop] != ""){ // but prop exists at least in a and is not empty
                return false;
            }
            else if(prop in b && b[prop] != ""){ // or exists at least in b and is not empty
                return false;
            }
        }
    }
    return true; // if nothing bad happened during the loop 
}



You must check if some values can be omitted. For example, in data1 you can have the following property balcony: "no" and in data2 balcony: "" (or balcony: "0" or no balcony property at all). Your script should not consider this as differences, because these 4 entries have all the same meaning (there is no balcony).

In the above code, I check if the property is not empty. It's the least you can do, but you will probably need to add other conditions, because this kind of database is necessarily filled by hand at some point, which means that different entries can be synonyms of one another.

This post has been edited by ge∅: 04 August 2014 - 06:36 AM

Was This Post Helpful? 0
  • +
  • -

#8 tenkepadu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 20-July 14

Re: How to do the right code of comparing two objects in pure/native JavaS

Posted 05 August 2014 - 07:07 PM

It's working. Thank you for your help! +1
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1