简体   繁体   English

如何比较两个不同的javascript对象具有相同的属性(不比较实例函数)

[英]How to compare two different javascript objects for same attributes (without comparing the instance function)

How do I compare the tree structure inside an object task with the obj_after tree, without having to actually compare individual nodes in the tree. 如何将对象任务中的树结构与obj_after树进行比较,而不必实际比较树中的各个节点。 (one is an instance of a class ie with instance methods and the other has no functions just data). (一个是类的实例,即具有实例方法,而另一个则不具有数据功能)。

Please read below to understand my exact problem (not so clear from the above). 请阅读以下内容,以了解我的确切问题(从上面不太清楚)。

I am writing a script that builds a GUI based on a js object tree. 我正在编写一个基于js对象树构建GUI的脚本。 I am using coffeescript for this. 我正在为此使用coffeescript。

I would like to create a class that takes a json, and build the tree. 我想创建一个接受json的类,并构建树。

window.GuiTree = class GuiTree
    constructor:(json_GUI_tree)->

But the problem I am having is in testing this. 但是我遇到的问题是测试它。 I would like to add methods to this class like 'add_node'. 我想向此类添加方法,例如“ add_node”。 That adds a new node to the GuiTree. 这将向GuiTree添加一个新节点。

So I try this in Jasmine. 所以我在茉莉花中尝试。

obj_before =
  1:
    type:"text" 
    name:"task"
    label:"Task"
  2:
    type:"subsection"
    sections:
      3:
        4:
          type:"text" 
          name:"subtask"
          label:"Subtask"   


obj_after =
  1:
    type:"text" 
    name:"task"
    label:"Task"
  2:
    type:"subsection"
    sections:
      3:
        4:
          type:"text" 
          name:"subtask"
          label:"Subtask"   
        5:
          type:"text" 
          name:"subtask"
          label:"Subtask"

Where I have added the task 5 to the Gui tree 我将任务5添加到Gui树的位置

And I would like to test it like this 我想这样测试

task = new GuiTree(obj_before)
expect(task.add(3, node_5)).givesNewTree(obj_after)

The matcher 'givesNewTree' is a custom matcher. 匹配器“ givesNewTree”是一个自定义匹配器。

The problem is here !! 问题在这里! how do I compare the tree structure inside the object task with the obj_after tree, without having to actually compare individual nodes in the tree. 如何将对象任务中的树结构与obj_after树进行比较,而不必实际比较树中的各个节点。 (one is ) (一个是)

To me it looks like lots of error prone code to write to compare the trees. 在我看来,要编写比较树的代码似乎很容易出错。 So I may need to write tests to test. 因此,我可能需要编写测试进行测试。 Is there a smarter way. 有没有更聪明的方法。

What you're looking for is an implementation of deep equivalence. 您正在寻找的是深度等效的实现。 They're not necessarily error prone, in fact there are several known implementation, the most well known is QUnit's deepEqual implementation. 它们不一定易于出错,实际上有几种已知的实现,其中最著名的是QUnit的deepEqual实现。 Here's another. 这是另一个 Granted it's a quite complicated algorithm. 当然,这是一个非常复杂的算法。

Your could of course also define a .equals (or similarly named) method in your GuiTree class that compares two instances in a simpler way based on what you already know is significant about them, but I would try existing deep equal algorithms first since you might be able to import previously written code that you already know is correct. 当然,您还可以在GuiTree类中定义一个.equals (或类似名称)方法,该方法根据您已经知道的关于它们的重要信息以一种更简单的方式比较两个实例,但是我会首先尝试使用现有的深层相等算法,因为您可能能够导入您已经知道正确的先前编写的代码。

A hackish alternative is to do JSON.stringify() on both objects and expect the resulting JSON encoded strings to be equal, but I suspect that could lead to various problems with circular references and the like, but it might get you started. 一种JSON.stringify()选择是在两个对象上执行JSON.stringify()并期望得到的JSON编码字符串相等,但是我怀疑这可能导致循环引用等各种问题,但这可能会让您入门。

Just today, Underscore.js 1.2.0 came out, with a new _.isEqual function that can handle cycles. 就在今天, _.isEqual 1.2.0发布了,它带有一个新的_.isEqual函数,可以处理周期。 Underscore is a robust, mature, and reasonably small library that works in both client-side and server-side JavaScript. Underscore是一个健壮,成熟且相当小的库,可在客户端JavaScript和服务器端JavaScript中使用。

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

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