2013-08-22 81 views
5

我有2個陣列,一個是的newval,另一個是origVal定義比較2個陣列,下劃線的對象找到differnce

原稿:

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

新:

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

如果我更改新的評分之一,我如何檢測該更改並檢索更改的對象?

一次只會有一個變化,但我認爲不重要。

FYI:這些陣列正在從一個Anjularjs watchcollection

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

}, true); 

生產謝謝 斯蒂芬

+0

是那些陣列(即''===身份)在同一個對象還是僅僅是相似的外觀? – Bergi

+0

你需要什麼結果?數組中已更改對象的索引,新更改對象,舊更改對象,評級? – Bergi

+0

我更喜歡改變的對象ie {「ListingId」:1762276,「Rating」:2,「ListPrice」:7411828,「PropertyType」:「Residential」} –

回答

10

看看這個:

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

的代碼是基於原有的功能difference從底線,但執行使用isEqual對象之間有很深的比較。

+0

並感謝您使用JSON.stringify的調試技巧 –

+1

, 它能做什麼。我希望我能理解代碼。 – mcktimo

3

如果訂單沒有變化,一個簡單的迭代將做到這一點。下劃線提供find method此任務:

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

如何:

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