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

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

问题描述

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

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

编写一个函数,deepEqual,它接受两个值,并且只有当它们取值时才返回真是相同的值或者是具有相同属性的对象,它们的值也是与递归调用 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.

另外,如果在 y 上没有找到 prop 属性,则返回 false(也就是说,计数匹配,但不是实际属性).

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天全站免登陆