i thought after deleting the element in the collection array that matches the condition , then the collection array should not contain it again. but i keep getting that that error “cannot read property hasOwnProperty of undefined” please explain more
Instead of removing things from the input array, what if you add things to the output array? Mutating an object as you iterate over it can be tricky and it’s causing you trouble here.
but since the conditional statements has been initiated there is no way arr[3] will give a value rather it will throw error. in my code above the conditional statement is correct. please explain more
The fact that you have this line in an if statement does not prevent an error from being thrown:
!collection[i].hasOwnProperty(arrSource[j])
collection[i] does not exist for part of the for loop, because, as others have mentioned, you are mutating the array, so the error you mentioned in your original post gets thrown, because you cannot check the property (perform .hasOwnProperty()) on an item that does not exist.