Javascript:深度比较 [英] Javascript: Deep Comparison

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

问题描述

我正在检查此问题 Javascript深度比较
提问者的解决方案没有说服我所以我试图分析问题,并提出了

I was checking this question Javascript Deep Comparison The solution of the question asker did not convince me so I tried to analyze the problem and came up with that

var obj = {here: 2};
console.log(deepEqual(obj, obj));
// → true
console.log(deepEqual(obj, {here: 1}));
// → false
console.log(deepEqual(obj, {here: 2}));
// → true
function deepEqual(a,b)
{
  if( (typeof a == 'object' && a != null) &&
      (typeof b == 'object' && b != null) )
  {
     var count = [0,0];
     for( var key in a) count[0]++;
     for( var key in b) count[1]++;
     if( count[0]-count[1] != 0) {console.log('1');return false;}
     for( var key in a)
     {
       if(!(key in b) || !deepEqual(a[key],b[key])) {console.log('2');return false;}
     }
     for( var key in b)
     {
       if(!(key in a) || !deepEqual(b[key],a[key])) {console.log('3');return false;}
     }  
  }
  console.log('a:'+a+' b:'+b);
  return a===b;
}
obj === { here:2 }

此代码失败最后一个测试(console.log(deepEqual(obj,{here:2})))但是如果它们分别具有相同的键和值,尽管在内存中存在不同的实例,但是考虑对象的逻辑非常相等并不能说服我。我的解决方案是否存在问题,或者错误在于演习的假设?我链接的问题中提到的代码是否有效?

This code fails the last test ( console.log(deepEqual(obj, {here: 2})) ) but the logic of considering objects deeply equal if they have respectively equal keys and values despite being different instances in memory does not convince me. Is there's a problem with my 'solution' or the mistake lies in the presumptions of the exercise? Is the code mentioned in the question I linked valid?

hikinthru忘记提及的资源( http: //eloquentjavascript.net/04_data.html#exercise_deep_compare

Resources that hikinthru forgot to mention ( http://eloquentjavascript.net/04_data.html#exercise_deep_compare )

推荐答案

深度平等,这是什么你所链接的问题正在谈论,而严格平等是两回事。正如你所说,深层平等意味着平等的钥匙和同等的价值观。对象的严格相等意味着相同的实例。严格的平等意味着深层次的平等,但是对象可以在不严格相等的情况下完全相等。

"Deep equality", which is what the question you linked to is talking about, and "strict equality" are two different things. "Deep equality" means, as you said, "equal keys and equal values." "Strict equality" for objects means "same instance." Strict equality implies deep equality, but objects can be deeply equal without being strictly equal.

你的代码效率有点低,因为你只需要一个循环,但如果你需要一个循环,它会表现正常你在 else 块中检查 a === b 并且返回true 之后的循环。这是因为您应该与原始值(如字符串和数字)分开处理对象。为了清楚起见,我删除了一些日志记录,我试图保持你的风格。

Your code is somewhat inefficient because you only need one loop, but it will behave correctly if you check a === b in an else block and return true after the for loops. This is because you should be handling objects separately from primitive values like strings and numbers. I've removed some logging for the sake of clarity, and I tried to keep your style.

var obj = {here: 2};
console.log(deepEqual(obj, obj));
// → true
console.log(deepEqual(obj, {here: 1}));
// → false
console.log(deepEqual(obj, {here: 2}));
// → true
console.log(obj === { here:2 });
// → false
function deepEqual(a,b)
{
  if( (typeof a == 'object' && a != null) &&
      (typeof b == 'object' && b != null) )
  {
     var count = [0,0];
     for( var key in a) count[0]++;
     for( var key in b) count[1]++;
     if( count[0]-count[1] != 0) {return false;}
     for( var key in a)
     {
       if(!(key in b) || !deepEqual(a[key],b[key])) {return false;}
     }
     for( var key in b)
     {
       if(!(key in a) || !deepEqual(b[key],a[key])) {return false;}
     }
     return true;
  }
  else
  {
     return a === b;
  }
}

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

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