DarkW1nter DarkW1nter - 3 years ago 117
Javascript Question

knockout.js compare 2 arrays without looping through them both

I have 2 observable arrays and within an if statement I want to do something only if the arrays are identical, is there any way to do this without looping through each?

Answer Source

You can call ko.toJSON on both arrays and then compare the json strings that are returned. Functionally that's probably just as much work if not more for the processor as looping through both arrays but it does look cleaner if that's all you're going for.

isEqual = ko.toJSON(aryA) === ko.toJSON(aryb)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download