user4217999
user4217999

Reputation:

How to compare two objects

I have a scenario, where I have two different Objects.

Scenario to achieve:

From two objects I need to match the values which has "A1","B2", etc...

Since both the objects values are not in proper order, the loop is breaking and missing some values.

In my demo the object1 has same repeated value i.e. "C3", It should be displayed only once.

Final output required is I need to detect only the matched values from two objects and display its corresponding "a" and "b values."

I have tried almost 90%, but somewhere some minor error is breaking my loop, Please help me out.

Sample code:

for(var i=0;i<obj1.results[0].loc.length;i++){
    var findA = obj1.results[0].loc[i].anc[0].title;
    for(var j=0;j< obj2.ILoc.length;j++){
        var findB = obj2.ILoc[j].ais;
        if(findA == findB) {
            var a = obj1.results[0].loc[i].a;
            var b = obj1.results[0].loc[i].b;
            console.log(a);
            console.log(b);
        }       
    }
}

This is what I have tried: Demo Link

Upvotes: 1

Views: 207

Answers (2)

halfzebra
halfzebra

Reputation: 6797

I would recommend using for...in loop, since you're using objects instead of arrays.

for (variable in object) {...
}

If length property of both objects is equal, then this kind of loop alone will help you to compare objects with ease.

Upvotes: 1

vanthome
vanthome

Reputation: 4926

I would recommend using the diff module. You can use it in node.js and the browser.

Upvotes: 0

Related Questions