2016-07-28 54 views
3

最近我正在開發該項目。我在解決兩個JavaScript對象之間的差異時遇到了問題。以下是該對象的兩個示例。第二個對象有一個額外的鍵。那麼如何區分這些對象呢?如何在javaScript中的兩個對象中獲得不同

實施例1對

{ 
    "title": "JavaScript Tutorial", 
    "body": "JavaScript", 
    "comments": [ 
    { 
     "name": "John Smith", 
     "comment": "Great article", 
     "age":  28, 
     "stars": 4, 
     "date": "2014-09-01" 
    }, 

    ] 
} 

實施例2

{ 
    "title": "JavaScript tutorial", 
    "body": "JavaScript.", 
    "topics":"how learn JavaScript withing 10 days", 
    "comments": [ 
    { 
     "name": "John Smith", 
     "comment": "Great article", 
     "age":  28, 
     "stars": 4, 
     "date": "2014-09-01" 
    }, 

    ] 
} 

欲導致像的下方。 下面是兩個對象

「主題」關鍵的不同:「如何學習JavaScript withing10天」,

+1

你嘗試過什麼? – nicael

+2

post expected result –

+0

當兩個對象比較兩個對象時,我想要兩個對象中的不同鍵 –

回答

0

看一看deep-diff,你基本上可以這樣使用您的樣本數據

var diff = require('deep-diff').diff; 

var lhs = { 
    "title": "JavaScript Tutorial", 
    "body": "JavaScript", 
    "comments": [ 
    { 
     "name": "John Smith", 
     "comment": "Great article", 
     "age":  28, 
     "stars": 4, 
     "date": "2014-09-01" 
    }, 

    ] 
}; 

var rhs = { 
    "title": "JavaScript tutorial", 
    "body": "JavaScript.", 
    "topics":"how learn JavaScript withing 10 days", 
    "comments": [ 
    { 
     "name": "John Smith", 
     "comment": "Great article", 
     "age":  28, 
     "stars": 4, 
     "date": "2014-09-01" 
    }, 

    ] 
}; 

var differences = diff(lhs, rhs); 
console.log(differences); 

將輸出

[ DiffEdit { 
    kind: 'E', 
    path: [ 'title' ], 
    lhs: 'JavaScript Tutorial', 
    rhs: 'JavaScript tutorial' }, 
    DiffEdit { 
    kind: 'E', 
    path: [ 'body' ], 
    lhs: 'JavaScript', 
    rhs: 'JavaScript.' }, 
    DiffNew { 
    kind: 'N', 
    path: [ 'topics' ], 
    rhs: 'how learn JavaScript withing 10 days' } ] 

正如你所看到的,也有隻比新的屬性更多的變化。 kind屬性也表明了這種差異。如果你只需要新的屬性,那麼只需考慮與kind === 'N'

0

對象如果遞歸不是一個因素,你可以循環兩個檢查屬性,如果兩者都擁有相應的屬性。

確保你分別循環兩次,這樣你就不會有任何誤報。

試試這個代碼出

function sameKeys (obj1, obj2) { 
    let diffs = [] 

    for (let prop in obj1) { 
     if (undefined === typeof obj2[prop] 
      || obj2[prop] !== obj1[prop]) { 

      diffs.push({ 
       type: (obj2[prop] !== undefined ? "Not equal" : "Undefined"), 
       field: prop, 
       whichOne: "Object 2", 
       values: { 
        object1: obj1[prop], 
        object2: obj2[prop] 
       } 
      }) 
     } 
    } 

    for (let prop in obj2) { 
     if (undefined === typeof obj1[prop] 
      || obj1[prop] !== obj2[prop]) { 

      diffs.push({ 
       type: (obj1[prop] !== undefined ? "Not equal" : "Undefined"), 
       field: prop, 
       whichOne: "Object 1", 
       values: { 
        object1: obj1[prop], 
        object2: obj2[prop] 
       } 
      }) 
     } 
    } 

    return diffs 
} 
+0

如果屬性存在但是具有錯誤值,'!obj2 [prop]'不會給出正確的結果。 – RobG

+0

好點查看我的編輯。 – 2016-07-30 00:13:42

+0

如果* obj1 [prop] *的值爲undefined *,它將無法正常工作。最好使用* hasOwnProperty *檢查或使用* Object.keys *來檢查自己的屬性。 * for..in *也遍歷繼承的可枚舉屬性。 – RobG

相關問題