typescript 如何通过两个对象进行比较打字稿angular6

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52049872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:36:54  来源:igfitidea点击:

How to compare by two objects typescript angular6

javascriptarraysangulartypescript

提问by Sevensnake77787

I want to compare two Json object arrays. One has more data. For the final result if one id finds another the selected checkbox = true. So far there are 4 and it only finds one. Do I loop through the long array first and then loop through the second array to find a match?

我想比较两个 Json 对象数组。一个有更多的数据。对于最终结果,如果一个 id 找到另一个,则选中的复选框 = true。到目前为止有 4 个,它只找到了一个。我是否先循环遍历长数组,然后循环遍历第二个数组以找到匹配项?

 this.formAll = JSON.parse(response)
      for (var i = 0; i < this.formAll.length; i++) {
        for (var j = i; j <  this.formAb.length; j++) {
         console.log( this.formAb[j].id,  this.formAll[i].SeriesNumber);
          if ( this.formAll[i].id === this.formAb[j].id) {
            console.log( 'small=', this.formAb[j].id, 'large=', 
         this.formAll[i].id );
         this.formAll[i].selected = true;
        }}
      }

回答by Thilina Sampath

Fast and limited

快速而有限

JSON.stringify(obj1) === JSON.stringify(obj2)

Slow and more generic

缓慢且更通用

Object.equals = function( x, y ) {
    if ( x === y ) return true;
    // if both x and y are null or undefined and exactly the same

    if ( ! ( x instanceof Object ) || ! ( y instanceof Object ) ) return false;
    // if they are not strictly equal, they both need to be Objects

    if ( x.constructor !== y.constructor ) return false;
    // they must have the exact same prototype chain, the closest we can do is
    // test there constructor.

    for ( var p in x ) {
        if ( ! x.hasOwnProperty( p ) ) continue;
        // other properties were tested using x.constructor === y.constructor

       if ( ! y.hasOwnProperty( p ) ) return false;
       // allows to compare x[ p ] and y[ p ] when set to undefined

       if ( x[ p ] === y[ p ] ) continue;
       // if they have the same strict value or identity then they are equal

       if ( typeof( x[ p ] ) !== "object" ) return false;
       // Numbers, Strings, Functions, Booleans must be strictly equal

       if ( ! Object.equals( x[ p ],  y[ p ] ) ) return false;
       // Objects and Arrays must be tested recursively
   }

   for ( p in y ) {
      if ( y.hasOwnProperty( p ) && ! x.hasOwnProperty( p ) ) return false;
      // allows x[ p ] to be set to undefined
    }
    return true;
}

回答by Adonias Vasquez

For Angular use Deep Equal

对于 Angular 使用 Deep Equal

import * as deepEqual from "deep-equal";

console.log(deepEqual({a:123},{a:123})); // True

回答by Mahendra Hirapra

You can achieve the result using the for-of and find method like :

您可以使用 for-of 和 find 方法获得结果,例如:

for(let item  of this.formAll) {
    if (this.formAb.find((i) => { i.id === item.id})){
       item.selected = true;
    }
}

回答by Sevensnake77787

this worked out better using filter.

使用过滤器效果更好。

   this.formAll = JSON.parse(response)
    this.formAll.forEach((element) => {
      this.formAb.filter((element1) => {
        if (element1.id === element.id ) {
          element.selected = true;
        }
     })
     })