Comparer 2 tableaux d'objets avec un trait de Soulignement pour trouver la differnce
J'ai 2 tableaux, l'un est newVal et l'autre est origVal définir
orig:
[
{"ListingId":1762276,"Rating":3,"ListPrice":7411828,"PropertyType":"Residential"},
{"ListingId":1826692,"Rating":3,"ListPrice":650000,"PropertyType":"Residential"},
{"ListingId":1833283,"Rating":4,"ListPrice":950000,"PropertyType":"Residential"},
{"ListingId":1832134,"Rating":3,"ListPrice":850000,"PropertyType":"Residential"},
{"ListingId":1829932,"Rating":4,"ListPrice":750000,"PropertyType":"Residential"},
{"ListingId":1827548,"Rating":5,"ListPrice":650000,"PropertyType":"Residential"}
]
nouveau:
[
{"ListingId":1762276,"Rating":2,"ListPrice":7411828,"PropertyType":"Residential"},
{"ListingId":1826692,"Rating":3,"ListPrice":650000,"PropertyType":"Residential"},
{"ListingId":1833283,"Rating":4,"ListPrice":950000,"PropertyType":"Residential"},
{"ListingId":1832134,"Rating":3,"ListPrice":850000,"PropertyType":"Residential"},
{"ListingId":1829932,"Rating":4,"ListPrice":750000,"PropertyType":"Residential"},
{"ListingId":1827548,"Rating":5,"ListPrice":650000,"PropertyType":"Residential"}
]
Si je change l'un des cotes de nouveau, comment pourrais-je détecter ce changement, et de récupérer l'objet modifié?
Il n'y aura qu'un seul changement à la fois, bien que je ne pense pas que les questions.
Pour info: Ces tableaux sont produites à partir d'un Anjularjs watchcollection
$scope.$watchCollection('items', function (new, old) {
}, true);
Merci,
Stephen
- Sont-ce les mêmes objets dans le tableau (c
===
identité) ou sont-ils seulement un aspect similaire? - Quels résultats avez-vous besoin? L'index de l'objet modifié dans le tableau, le nouveau changé d'objet, l'ancien changé d'objet, la notation?
- Je préfère l'objet modifié ie {"ListingId":1762276,"Rating":2,"ListPrice":7411828,"PropertyType":"Résidentiel"}
Vous devez vous connecter pour publier un commentaire.
Prendre un coup d'oeil à ceci:
Le code est basé sur la fonction d'origine différence de trait de soulignement, mais il effectue une comparaison approfondie entre les objets à l'aide de isEqual.
Si l'ordre ne change pas, une simple itération de le faire. Trait de soulignement fournit la
trouver
méthode pour cette tâche:Comment sur: