2013-08-22 17 views
5

tôi có 2 mảng, một là newVal và thứ hai là origVal xác địnhHãy so sánh 2 mảng của các đối tượng với gạch để tìm ra 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"} 
] 

mới:

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

Nếu tôi thay đổi một trong các xếp hạng mới, làm cách nào tôi có thể phát hiện thay đổi đó và truy lục đối tượng đã thay đổi?

Sẽ chỉ có một thay đổi tại một thời điểm, mặc dù tôi không nghĩ rằng vấn đề đó là quan trọng.

FYI: Những mảng đang được sản xuất từ ​​một watchcollection Anjularjs

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

}, true); 

Cảm ơn bạn, Stephen

+0

Là những đối tượng tương tự trong mảng (ví dụ: '===' danh tính) hoặc là họ chỉ tương tự tìm? – Bergi

+0

Bạn cần kết quả gì? Chỉ số của đối tượng đã thay đổi trong mảng, đối tượng đã thay đổi mới, đối tượng đã thay đổi cũ, xếp hạng? – Bergi

+0

Tôi muốn đối tượng thay đổi tức là {"ListingId": 1762276, "Xếp hạng": 2, "ListPrice": 7411828, "PropertyType": "Residential"} –

Trả lời

10

Hãy xem này:

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

Mã này được dựa trên các chức năng ban đầu difference từ gạch dưới, nhưng nó thực hiện một sự so sánh sâu giữa các đối tượng sử dụng isEqual .

+0

Và cảm ơn bạn về mẹo gỡ lỗi với JSON.stringify –

+1

Thật tuyệt vời , Những gì nó làm. Tôi ước tôi có thể hiểu được mã. – mcktimo

3

Nếu thứ tự không thay đổi, một sự lặp lại đơn giản sẽ làm điều đó. Gạch cung cấp find method cho nhiệm vụ này:

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

Làm thế nào về:

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) 
})