2013-08-22 8 views
5

Mam 2 tablice, jedna jest newval a druga jest origVal określićporównaj 2 tablice obiektów podkreślenia znaleźć differnce

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"} 
] 

nowy:

[ 
{"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"} 
] 

Jeśli zmienię jedną z ocen w nowym, jak mogę wykryć tę zmianę i pobrać zmieniony obiekt?

Będzie tylko jedna zmiana na raz, chociaż nie sądzę, aby to miało znaczenie.

FYI: Te tablice są produkowane z watchcollection Anjularjs

$scope.$watchCollection('items', function (new, old) { 

}, true); 

Dziękuję Stephen

+0

Czy te same obiekty w tablicy (to znaczy '' === tożsamości) lub są one jedynie podobne wyglądzie? – Bergi

+0

Jaki wynik potrzebujesz? Indeks zmienionego obiektu w tablicy, nowy zmieniony obiekt, stary zmieniony obiekt, ocena? – Bergi

+0

Wolałbym zmieniony obiekt tj. {"ListingId": 1762276, "Rating": 2, "ListPrice": 7411828, "PropertyType": "Residential"} –

Odpowiedz

10

spojrzeć na to:

var a = [ 
{"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"} 
]; 

var b = [ 
{"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"} 
] 

var difference = function(array){ 
    var rest = Array.prototype.concat.apply(Array.prototype, Array.prototype.slice.call(arguments, 1)); 

    var containsEquals = function(obj, target) { 
    if (obj == null) return false; 
    return _.any(obj, function(value) { 
     return _.isEqual(value, target); 
    }); 
    }; 

    return _.filter(array, function(value){ return ! containsEquals(rest, value); }); 
}; 

console.log(JSON.stringify(difference(b, a))); 
> [{"ListingId":1762276,"Rating":2,"ListPrice":7411828,"PropertyType":"Residential"}] 

Kod jest oparty na oryginalnej funkcji difference od podkreślenia, ale wykonuje głęboki porównanie obiektów wykorzystujących isEqual .

+0

I dziękuję za wskazówkę do debugowania za pomocą JSON.stringify –

+1

Jest genialny , co to robi. Chciałabym zrozumieć kod. – mcktimo

3

Jeśli zamówienie nie zmienia, prosta iteracja to zrobi. Podkreślenia zapewnia find method dla tego zadania:

var changedObj = _.find(newVal, function(obj, index) { 
    return obj.Rating != oldVal[index].Rating; 
}); 
0

Jak o:

const current = [{name:'a'},{name:'b'},{name:'c'}] 
const update = [{name:'x'},{name:'a'},{name:'b'},{name:'f'}] 
const records = current.concat(update); 
const diff = {}; 
diff.in = [] 
diff.out = []; 
records.forEach(item => { 
    if(!current.find(cur => item.name == cur.name))diff.in.push(item) 
    if(!update.find(up => item.name == up.name))diff.out.push(item) 
}) 
Powiązane problemy