简体   繁体   English

返回不相等的Object.keys

[英]Object.keys returning as not equal

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). 我正在尝试将一个对象的键与另一个对象中的键进行比较(这是针对freeCodeCamp算法的)。 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 当source参数中有多个键时,

if (collection[i].hasOwnProperty(Object.keys(source)))

returns false even when collection[i] does contain the keys as per below. 即使collection [i]确实包含以下键,也将返回false。

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? 我的问题是,为什么a,b和a,b不相等? Thank you for your time! 感谢您的时间!

FreeCodeCamp is great. FreeCodeCamp很棒。 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 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 从MDN: https//developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/is https://developer.mozilla.org/en-US/docs/Web/JavaScript/ Equality_comparisons_and_sameness

I'm not sure that hasOwnProperty is what you want. 我不确定hasOwnProperty是否是您想要的。 Check out the definition of hasOwnProperty : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty 查看hasOwnProperty的定义: https : //developer.mozilla.org/zh-CN/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 您可以遍历Object.keys(source)并将其与Object.keys(collection[i]) ,或者尝试更短的尝试,请参阅: 确定一个数组是否包含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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM