【问题标题】:compare two objects without object reference Angular 10比较没有对象引用Angular 10的两个对象
【发布时间】:2020-11-07 20:46:00
【问题描述】:

我想比较两个对象,看看它们是否相等。 对象 A (offer) 是原始对象,对象 B (offerTemp) 是 A 的副本。

对象 A 显示在组件的表中,以查看表中是否有任何更改,我想将 A 与 B 进行比较。但是当我调用函数检查它们是否相等时,它说对象是不相等。我认为这是因为对象 A 具有该对象的引用而 B 没有(至少这是我在控制台中记录两个对象时看到的)。

对象 A(报价)(来自控制台):

Offer {id: 1834, title: "A great article offer - 1834", description: "browser", sellDate: Mon Dec 25 2045 21:54:06 GMT+0100 (Central European Standard Time), valueHighestBid: 753, …}

对象 B (offerTemp)(来自控制台):

{id: 1834, title: "A great article offer - 1834", description: "browser", sellDate: Mon Dec 25 2045 21:54:06 GMT+0100 (Central European Standard Time), valueHighestBid: 753, …}

这是来自component.ts

ngOnInit(): void {
    this.offer = this.offerService.FindById(this.editOfferId);
    this.offerTemp = Object.assign({}, this.offer);
  }

cancelSelected(offer: Offer): void{
    if (offer === this.offerTemp) {
    // do stuff...
    }
    else {
      console.log(this.offer === this.offerTemp); // false in the console
      console.log(this.offer);
      console.log(this.offerTemp);
    }
  }

这是我的html部分:

<div>
  <table>
    <tr> 
      <th>Selected offer details: (id: {{offer.id}})</th>
    </tr>
    <tr>
      <td>Title</td>
      <td>
        <input [(ngModel)]="offer.title" type="text" name="" id="">
      </td>
    </tr>
    <tr>
      <td>description</td>
      <td>
        <input [(ngModel)]="offer.description" type="text" name="" id="">
        </td>
    </tr>
    <tr>
      <td>auctionStatus</td>
      <td>
        <select>
          <option *ngFor="let key of keys" [value]="key" [selected]="offer.auctionStatus">{{statusen[key]}}</option>
        </select>
      </td>
    </tr>
    <tr>
      <td>valueHighestBid</td>
      <td>
        <input [(ngModel)]="offer.valueHighestBid" type="text" name="" id="">
     </td>
    </tr>

  </table>
  <button mat-raised-button (click)="deleteSelected()"> Delete </button>
  <button mat-raised-button (click)="saveSelected(offer)"> Save </button>
  <button mat-raised-button (click)="clearSelected(offer)" > Clear </button>
  <button mat-raised-button (click)="resetSelected(offer)" > Reset </button>
  <button mat-raised-button (click)="cancelSelected(offer)"> Cancel </button>


</div>

有没有什么方法可以在没有对象引用的情况下比较这些对象或我可以解决这个问题的其他方法?

【问题讨论】:

    标签: javascript angular comparison object-comparison


    【解决方案1】:

    JavaScript 没有用于对象和数组的内置属性相等运算符。

    检查对象是否相等的一种简单方法是使用 JSON.stringify:

    JSON.stringify(objectA) === JSON.stringify(objectB);
    

    这会将对象转换为字符串并使其易于比较。这种方法在对象嵌套时也很有效。

    另一种选择是使用 equals 方法(或者更好的是也适用于嵌套对象的深度相等方法),它遍历所有对象的属性并比较它们的值。

    【讨论】:

    • 如果某些值是函数类型,这个将无法正常工作。相反,需要进行深入比较。
    【解决方案2】:

    仅当两个变量引用同一个对象时,使用三个等号=== 比较两个对象才会返回true。你显然不希望这样。

    您需要创建自己的对象比较方式,或者使用一些提供对象深度比较的 JavaScript 库,例如来自 lodash 的_.isEqual

    let object = { 'a': 1 };
    let other = { 'a': 1 };
     
    _.isEqual(object, other);
    // => true
     
    object === other;
    // => false
    

    【讨论】:

    • 感谢您的回复。两个答案都有效。我将从 lodash 中查看 lib
    猜你喜欢
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-22
    • 2019-08-02
    • 2014-01-26
    • 2012-08-17
    相关资源
    最近更新 更多