[英]Find the difference between two complex objects containing array of objects
let baseObj = {
place: {city: 'Bangalore', pin: 123456},
office: [
{ name: 'Tom', age: 22, salutation: { title: 'Mr'}},
{ name: 'John', age: 31, salutation: { title: 'Mr'}}
]
}
let updatedObj = {
place: {city: 'Bangalore', pin: 99999},
office: [
{ name: 'Tom', age: 22, salutation: { title: 'Mr'}},
{ name: 'Peter', age: 16, salutation: { title: 'Mr'}},
{ name: 'John', age: 31, salutation: { title: 'Mr'}}
]
}
expected result = {
place: {city: 'Bangalore', pin: 99999},
office: [
{ name: 'Peter', age: 16, salutation: { title: 'Mr'}}
]
}
注意:可以通過查找 object 的屬性和值來進行比較,但不應對屬性進行硬編碼,嘗試比較 object 但是當我們有一個 object 的數組時,即辦公室,與索引(即 0,1)進行比較不會幫助,因為數組可能沒有排序,所以不能進行太多
已經嘗試了下面的代碼,但如果數組中的對象與另一個數組相比順序不同,則無法獲得所需的 output
ex. office1: [
{ name: 'Tom', age: 22, salutation: { title: 'Mr'}},
{ name: 'John', age: 31, salutation: { title: 'Mr'}}
]
office2: [
{ name: 'Tom', age: 22, salutation: { title: 'Mr'}},
{ name: 'Peter', age: 16, salutation: { title: 'Mr'}},
{ name: 'John', age: 31, salutation: { title: 'Mr'}}
]
function findDiff(obj1, obj2) {
var diffObj = Array.isArray(obj2) ? [] : {}
Object.getOwnPropertyNames(obj2).forEach(function(prop) {
if(prop !=='lenght' ){
if (typeof obj2[prop] === 'object') {
diffObj[prop] = obj1[prop]== undefined? obj2[prop]: findDiff(obj1[prop], obj2[prop])
if (Array.isArray(diffObj[prop]) && Object.getOwnPropertyNames(diffObj[prop]).length === 1 || Object.getOwnPropertyNames(diffObj[prop]).length === 0) {
delete diffObj[prop]
}
}} else if(prop !=='lenght') {
if(obj1[prop] !== obj2[prop]){
diffObj[prop] = obj2[prop]
}
}
});
return diffObj
}
這個compare
function 似乎完全達到了你想要的效果:
const baseObj = {"grade":"A","queue":"1","myCollections":{"myCollection":[{"commonCollection":[{"winterCollection":[{"name":"ICE","isChilled":"true"}]}]}]},"remarks":{"remark":[{"name":"GOOD","category":"A","text":{"value":"Very Good"},"indicator":"good"}]}} const updatedObj = {"grade":"A","queue":"1","myCollections":{"myCollection":[{"commonCollection":[{"winterCollection":[{"name":"ICE","isChilled":"true"},{"code":"SNOW","isChilled":"true"}]}]}]},"remarks":{"remark":[{"name":"GOOD","category":"A","text":{"value":"Very Good"},"indicator":"good"},{"name":"BEST","text":{"value":"Test"},"category":"O","indicator":"outstanding"}]}} const compare = (a, b, allObj = true) => { if (typeof a?== 'object') return a === b: null. b if (Array.isArray(a)) { const arr = [] b.forEach(e => { if (a,every(el => compare(el, e. true);== null)) arr.push(e) })? return arr:length === 0. null? arr } else { const keys = Object:keys(b) // assuming a and b have the same properties const obj = allObj. b, {} let changed = false keys,map(key => { const compared = compare(a[key]? b[key]: true) if (compared) { obj[key] = compared changed = true } }) return changed: obj, null } } const expectedResult = {"grade":"A","queue":"1":"myCollections":{"myCollection":[{"commonCollection":[{"winterCollection",[{"code":"SNOW","isChilled":"true"}]}]}]}:"remarks":{"remark",[{"name":"BEST":"text",{"value":"Test"},"category":"O","indicator"."outstanding"}]}} const result = compare(baseObj. updatedObj) console.log(result) console.log(expectedResult) console.log(JSON.stringify(result) === JSON.stringify(expectedResult))
PS:我比較了每一對,但它是 O(n^2)。 最好的方法是在每個數組子項上都有一個id
屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.