0

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!

Will
  • 1
  • 2
  • Thank you very much for taking the time to help! I did come up with a better way to solve the algorithm before posting this, I guess I should have been more clear on that part. I'm trying to understand why collection[0].hasOwnProperty(Object.keys(source)) equals false in the above when both collection[0] and Object.keys(source) are equal to a,b. – Will Jul 08 '17 at 04:23
  • Given that `whereAreYou([{ "a": 1 }, { "a": 1 }, { "a": 1, "b": 2 }], { "a": 1 }) Object.keys(source)= a Object.keys(collection[0]))= a collection[0].hasOwnProperty(Object.keys(source))= true Object.keys(source)= a Object.keys(collection[1]))= a collection[1].hasOwnProperty(Object.keys(source))= true Object.keys(source)= a Object.keys(collection[2]))= a,b collection[2].hasOwnProperty(Object.keys(source))= true []` why doesn't a,b return true? – Will Jul 08 '17 at 19:30

2 Answers2

0

FreeCodeCamp is great. If this is a specific question to some training they have, usually their forums are a pretty good outlet. They'll know more about what you're asking and what it pertains too. However, if you're just trying to compare objects, this seems quite verbose.

Check out some other posts on stack to see, here are a few links that help understand comparing objects. Object comparison in JavaScript

From MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness

Dylan Wright
  • 1,118
  • 12
  • 19
0

I'm not sure that hasOwnProperty is what you want. Check out the definition of hasOwnProperty: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty

You need to compare the items within each array. For example, on the first iteration:

Object.keys(collection[i]).includes("a") // true
Object.keys(collection[i]).includes("b") // true
Object.keys(collection[i]).includes(Object.keys(source)[0]) // true
Object.keys(collection[i]).includes(Object.keys(source)[1]) // true

You can either iterate over Object.keys(source) and compare it to Object.keys(collection[i]), or try something shorter, see: Determining whether one array contains the contents of another array in JavaScript/CoffeeScript

You can do something like this:

function whereAreYou(collection, source) {
    var arr = [];
    for (var i = 0; i < collection.length; i++) {
        for (var j = 0; j < Object.keys(source).length; j++) {
            if (Object.keys(collection[i]).includes(Object.keys(source)[j])) {
                // do your comparisons here
            }
        }
    }
    return arr;
}
ikcodez
  • 323
  • 2
  • 16