This is my first post, I have researched but I'm not sure I'm phrasing the question the right way in searches.
I am attempting to compare the keys of an object to the keys in another object (this is for freeCodeCamp algorithms). My code is below:
function whereAreYou(collection, source) {
var arr = [];
for (var i=0;i<collection.length;i++) {
console.log("Object.keys(source)= " + Object.keys(source));
console.log("Object.keys(collection[" +i + "]))= " + Object.keys(collection[i]));
console.log("collection[" +i + "].hasOwnProperty(Object.keys(source))= " + collection[i].hasOwnProperty(Object.keys(source)));
if (collection[i].hasOwnProperty(Object.keys(source))) {
var prop = Object.keys(source);
console.log("prop=" + prop);
console.log("collection[" +i + "][prop]= " + collection[i][prop]);
console.log("source[prop]= " + source[prop]);
if (collection[i][prop] === source[prop]) {
arr.push(collection[i]);
}
}
}
return arr;
}
When there are multiple keys in the source argument, the
if (collection[i].hasOwnProperty(Object.keys(source)))
returns false even when collection[i] does contain the keys as per below.
whereAreYou([{ "a": 1, "b": 2 }, { "a": 1 }, { "a": 1, "b": 2, "c": 2 }], { "a": 1, "b": 2 })
Object.keys(source)= a,b
Object.keys(collection[0]))= a,b
collection[0].hasOwnProperty(Object.keys(source))= false
Object.keys(source)= a,b
Object.keys(collection[1]))= a
collection[1].hasOwnProperty(Object.keys(source))= false
Object.keys(source)= a,b
Object.keys(collection[2]))= a,b,c
collection[2].hasOwnProperty(Object.keys(source))= false
[]
My question is why aren't a,b and a,b equal? Thank you for your time!