Javascript - deepEqual比较 [英] Javascript - deepEqual Comparison

查看:137
本文介绍了Javascript - deepEqual比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题(来自Eloquent Javascript第2版,第4章,练习4):

Question (From Eloquent Javascript 2nd Edition, Chapter 4, Exercise 4):


编写一个函数deepEqual,它接受两个值并且只有当
是相同的值时才返回true,或者是与对deepEqual的递归调用相比,具有相同属性且其值也是
的对象。

Write a function, deepEqual, that takes two values and returns true only if they are the same value or are objects with the same properties whose values are also equal when compared with a recursive call to deepEqual.

测试案例:

var obj = {here: {is: "an"}, object: 2};
console.log(deepEqual(obj, obj));
// → true
console.log(deepEqual(obj, {here: 1, object: 2}));
// → false
console.log(deepEqual(obj, {here: {is: "an"}, object: 2}));
// → true

我的代码:

var deepEqual = function (x, y) {
  if ((typeof x == "object" && x != null) && (typeof y == "object" && y != null)) {
    if (Object.keys(x).length != Object.keys(y).length)
      return false;
    for (var prop in x) {
      if (y.hasOwnProperty(prop))
        return deepEqual(x[prop], y[prop]);
    /*This is most likely where my error is. The question states that all the values
    should be checked via recursion; however, with the current setup, only the first
    set of properties will be checked. It passes the test cases, but I would like
    to solve the problem correctly!*/
      }
    }
  else if (x !== y)
    return false;
  else
    return true;
}

我认为我有一般的想法;但是,就像我在评论中所说的那样,程序不会检查对象中的第二个属性。我觉得我有结构/逻辑问题而且只是以错误的方式使用递归,因为我原本打算循环遍历属性,使用递归来比较第一个属性的值,然后继续循环到下一个属性财产并再次比较。虽然,我不确定这是否可能?

I think I have the general idea down; however, like I stated in the comment, the program will not check the second property in the objects. I feel like I have a structural/logic problem and am simply using recursion in the wrong way, as I originally intended to loop through the properties, use recursion to compare the values of the first property, then continue on in the loop to the next property and compare again. Although, I'm not sure if that's even possible?

我已经给了很多想法并尝试了几种不同的方法,但这是最正确的答案我到目前为止。是否有任何可能的提示让我指出正确的方向?

I've given a good amount of thought and tried a couple different approaches, but this was the most correct answer I've come to so far. Any possible tips to point me in the right direction?

推荐答案

正如您所怀疑的那样,您将返回第一个属性的匹配看到。如果该属性不匹配,您应该返回 false ,但请继续查看。

As you suspect, you're returning the match of the first property seen. You should return false if that property doesn't match, but keep looking otherwise.

此外,返回 false 如果在 y 上找不到 prop 属性(即,计数匹配,但不是实际属性。)

Also, return false if there's no prop property found on y (that is, the counts match, but not the actual properties).

如果所有属性都匹配,则返回 true

If all properties have matched, return true:

var deepEqual = function (x, y) {
  if (x === y) {
    return true;
  }
  else if ((typeof x == "object" && x != null) && (typeof y == "object" && y != null)) {
    if (Object.keys(x).length != Object.keys(y).length)
      return false;

    for (var prop in x) {
      if (y.hasOwnProperty(prop))
      {  
        if (! deepEqual(x[prop], y[prop]))
          return false;
      }
      else
        return false;
    }

    return true;
  }
  else 
    return false;
}

var deepEqual = function (x, y) {
  if (x === y) {
    return true;
  }
  else if ((typeof x == "object" && x != null) && (typeof y == "object" && y != null)) {
    if (Object.keys(x).length != Object.keys(y).length)
      return false;

    for (var prop in x) {
      if (y.hasOwnProperty(prop))
      {  
        if (! deepEqual(x[prop], y[prop]))
          return false;
      }
      else
        return false;
    }

    return true;
  }
  else 
    return false;
}

var obj = {here: {is: "an", other: "3"}, object: 2};
console.log(deepEqual(obj, obj));
// → true
console.log(deepEqual(obj, {here: 1, object: 2}));
// → false
console.log(deepEqual(obj, {here: {is: "an"}, object: 2}));
// → false
console.log(deepEqual(obj, {here: {is: "an", other: "2"}, object: 2}));
// → false
console.log(deepEqual(obj, {here: {is: "an", other: "3"}, object: 2}));
// → true

这篇关于Javascript - deepEqual比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆