1

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

isherwood
  • 58,414
  • 16
  • 114
  • 157

2 Answers2

1

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.

halfzebra
  • 6,771
  • 4
  • 32
  • 47
  • the length might be equal or might not. –  Jan 22 '15 at 19:26
  • There are plenty of algorithms for that kind of comparison, I would recommend checking http://stackoverflow.com/questions/1068834/object-comparison-in-javascript One of the fastest ways to compare objects is achieved through using hasOwnProperty method. – halfzebra Jan 22 '15 at 19:41
0

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

vanthome
  • 4,816
  • 37
  • 44
  • Is there no other way apart from node.js, if simple js/jquery solution there, it would be very helpful. –  Jan 22 '15 at 19:25