2016-06-13 10 views
6

Załaduj tablicę z pliku json, co 1,5 sekundy Sprawdzam, czy są jakieś zmiany w pliku (w tej chwili testuję jeden plik bez żadnych zmian), ale kiedy sprawdzić, czyDlaczego dwa równe obiekty wyświetlają "nie równy" w Angular 2

if (this.itemsParentArray[i] !== this.itemInArray[i]) 

zawsze pokazuje, że nie jest równy, a console.log („” nie równa się ")

czy brakowało mi coś w kodzie ?? Oto ona:

export class HomeComponent { 
itemsParentArray = []; 
itemInArray = []; 
myRes: Content; 
showAssigned:boolean = false; 

constructor(private structureRequest: StructureRequestService) { 
    setInterval(() => { 
     this.timerGetStructure(); 
    }, 1500); 
} 
// using with setInterval to get new data and sets into content Array with this.updateItems(result) if it's new 

timerGetStructure() { 
    this.structureRequest.sendRequest().subscribe((result) => this.updateItems(result)); 
} 
updateItems(result) { 
    this.myRes = result; 
    this.itemInArray = this.myRes.content; 
    for (let i = 0; i < this.itemInArray.length; i++) { 
       if (this.itemsParentArray[i] !== this.itemInArray[i]) { 
        // this.itemsParentArray[i] = this.itemInArray[i]; 
        console.log("not equal"); 
       } 
      } 
} 

// 
ngOnInit() { 
    //makes http request and puts result into parentArray after 3 sec. 
    this.structureRequest.sendRequest().subscribe((result) => this.viewNodes(result)); 
} 
//view items 
viewNodes(result) { 
    setTimeout(() => { 
     this.myRes = result; 
     this.itemsParentArray = this.myRes.content; 
     this.showAssigned = true; 
    }, 3000); 
} 
} 

Jak widać ładuje dane z tego samego pliku (nie zmieniać dane pliku !!!):

this.itemsParentArray = this.myRes.content; 

i (każde 1,5 sek):

this.itemInArray = this.myRes.content; 

Odpowiedz

2

Napisałem tę funkcję pomocniczą w maszynie:

export class Helper { 
    private _recursiveProperties: string[] = ['RecursiveProperty', ...]; 

    public equals(obj1: any, obj2: any): boolean { 
     if (typeof obj1 !== typeof obj2) { 
      return false; 
     } 
     if ((obj1 === undefined && obj2 !== undefined) || 
      (obj2 === undefined && obj1 !== undefined) || 
      (obj1 === null && obj2 !== null) || 
      (obj2 === null && obj1 !== null)) { 
      return false; 
     } 
     if (typeof obj1 === 'object') { 
      if (Array.isArray(obj1)) { 
       if (!Array.isArray(obj2) || obj1.length !== obj2.length) { 
        return false; 
       } 
       for (let i = 0; i < obj1.length; i++) { 
        if (!this.equals(obj1[i], obj2[i])) { 
         return false; 
        } 
       } 
      } else { 
       for (let prop in obj1) { 
        if (obj1.hasOwnProperty(prop)) { 
         if (!obj2.hasOwnProperty(prop)) { 
          return false; 
         } 
         //Endless loop fix for recursive properties 
         if (this._recursiveProperties.indexOf(prop) >= 0) { 
          if (obj1[prop] !== obj2[prop]) { 
           return false; 
          } 
         } else if (!this.equals(obj1[prop], obj2[prop])) { 
          return false; 
         } 
        } 
       } 
       for (let prop in obj2) { 
        if (obj2.hasOwnProperty(prop)) { 
         if (!obj1.hasOwnProperty(prop)) { 
          return false; 
         } 
        } 
       } 
      } 
      return true; 
     } 
     return obj1 === obj2; 
    } 
} 

Gdzie _recursiveProperties zawiera nazwy The (jeśli w ogóle) właściwości powodujące nieskończoną pętlę . Na przykład. Miałem obiekt (obj1) z właściwością, która zawierała odniesienie do innego obiektu (obj2), który z kolei zawierał odniesienie do obj1.

Jeśli ktoś ma lepsze rozwiązanie, prosimy o komentarz.

Wykorzystanie będzie wówczas:

let helper = new Helper(); 
if (helper.equals(this.itemsParentArray[i], this.itemInArray[i])) 
+0

Rozwiązaniem problemu z właściwościami rekursywnymi byłoby przechowywanie listy odwiedzanych nieruchomości i powtarzanie się tylko podczas odwiedzania nowych nieruchomości. – N8allan

1

Dla mnie porównanie dwóch obiektów kątowy 2/4 Próbowałem poniżej kodu. który całkowicie działał.

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

Powiązane problemy