需要比较相同类型的对象才能知道javascript中的差异

发布于 2024-10-14 11:45:57 字数 164 浏览 3 评论 0原文

我需要比较对象以了解更改了哪些属性以及添加了哪些新属性。 是否有任何代码片段将循环遍历整个对象及其属性,并在存在任何更改时发出警报,并继续其过程直到结束。

考虑我需要比较 extjs 中的 gridpanel(object) 在其父面板的显示和隐藏事件之前和之后。

提前致谢

I need to compare the object to know what properties are changed and what properties are added new.
Is there any code snippet which will loop through entire object and its properties and alert if any changes exist and continue its process till the end.

Consider i need to compare the gridpanel(object) in extjs before and after show and hide event of its parent panel.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肤浅与狂妄 2024-10-21 11:45:57

如果您只是想检查同一类的两个对象是否不同:

function differ(a, b) {
  for (var i in a) {
    if (a.hasOwnProperty(i) && b.hasOwnProperty(i) && a[i] === b[i]) {
      return true;
    }
  }
  return false;
}

a = {foo: 1, bar: 2};
b = {bar: 2};

alert( differ(a, b) ? "Objects differ" : "Objects are the same" );

如果这不是您想要的,请更详细地解释您的问题。

If you simply want to check if two objects of the same class differ:

function differ(a, b) {
  for (var i in a) {
    if (a.hasOwnProperty(i) && b.hasOwnProperty(i) && a[i] === b[i]) {
      return true;
    }
  }
  return false;
}

a = {foo: 1, bar: 2};
b = {bar: 2};

alert( differ(a, b) ? "Objects differ" : "Objects are the same" );

If that's not what you're after, explain your problem in more detail.

乖乖 2024-10-21 11:45:57

当我向数组添加对象时,我遇到了类似的问题。我想确保同一个对象不会添加两次。

我找到了 ExtJS 的辅助函数,它实现了一个根据对象包含的值来比较对象的函数:

https: //github.com/nmishra/helpers.js

然后可以像这样使用:

var obj1 = {foo: 'bar'},
    obj2 = {foo: 'bar'},
    equality = Ext.ux.util.Object.compareObject(obj1, obj2);
console.log(equality);

I had a similar problem when I was adding objects to an array. I wanted to be sure, that the same object is not added twice.

I found a helper function for ExtJS which implements a function that compares objects based on the values contained by the object:

https://github.com/nmishra/helpers.js

It then can be used like this:

var obj1 = {foo: 'bar'},
    obj2 = {foo: 'bar'},
    equality = Ext.ux.util.Object.compareObject(obj1, obj2);
console.log(equality);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文