Let’s work through the test case with your code.
The outer for loop starts with i = 0
In the first iteration of the inner for loop, x = 0, so your code checks if {“a”:1,“b”:2} has the property “a”. It does, so you add {“a”:1,“b”:2} to test.
In the second iteration of the inner for loop, x = 1, so your code checks if {“a”:1,“b”:2} has the property “b”. It does, so you add {“a”:1,“b”:2} to test.
The outer for loop now has i = 1
In the first iteration of the inner for loop, x = 0, so your code checks if {“a”:1} has property “a”. It does, so you add {“a”:1} to test.
In the second iteration of the inner for loop, x = 1, so your code checks if {“a”:1} has the property “b”. It does NOT, so you add “no” to test.
The outer for loop now has i = 2
In the first iteration of the inner for loop, x = 0, so your code checks if {“a”:1,“b”:2,“c”:2} has property “a”. It does, so you add {“a”:1,“b”:2,“c”:2} to test.
In the second iteration of the inner for loop, x = 1, so your code checks if {“a”:1,“b”:2,“c”:2} has the property “b”. It does, so you add {“a”:1,“b”:2,“c”:2} to test.
Your algorithm is only looking at each source property to see if it is in the current collection object being checked. Your solution is not keeping track whether or not ALL the source properties are in the current collection object being checked.
Like I said before, you need to write out your algorithm and walk through the code like I just did above, to prove out your algorithm works or does not work. You do not have to write full code for your algorithm steps. It can be plain language or a mix of plain language and code. The logic still has to work though.
I will give you something to think about for at least helping to identify if you should consider adding a collection object based strictly on the property name. You could create another variable (let’s call it propFound) before the inner for loop and assign the value of true to this variable. The reason you would assign it true, is you would be assuming that all of the source properties will be found. Then, inside the inner for loop, your code would need to check if the current source property does NOT exist in the current collection object. If the if statement evaluates to true (meaning the property does not exist), then you assign the value of false to the propFound variable and then break out of the inner for loop. The break will prevent any other source property from being checked. Just outside the inner for loop, you would check if propFound is true. If it is still true, that means all the source properties where found in the current collection object, so you should push the object to test. If not, do nothing.