2015-01-12 68 views
2

我要生成從兩個給定對象A中的對象和B,只有從B值從A.不同,我們可以假設,在A和B.獲取對象具有不同價值觀

存在的所有領域示例:假設以下兩種對象:

{ 
    "firstName": "John", 
    "lastName": "Doe", 
    "nickname": "Johnny", 
    "location": { 
     "latitude": 1.0, 
     "longitude": 1.0 
    }, 
    "email": "[email protected]" 
} 

{ 
    "firstName": "John", 
    "lastName": "Doe", 
    "nickname": "John-Boy", 
    "location": { 
     "latitude": 1.0, 
     "longitude": 2.0 
    }, 
    "email": "[email protected]" 
} 

暱稱和位置發生了變化,我希望得到的結果是:

{ 
    "nickname": "John-Boy", 
    "location": { 
     "latitude": 1.0, 
     "longitude": 2.0 
    } 
} 

注意,我想全location對象,而不僅僅是改變longitude

什麼將是實現這一目標的一個好辦法嗎?

+1

請參閱:[***如何獲得兩個JavaScript對象圖之間的差異列表?***(http://stackoverflow.com/questions/264430/how-can-i-get-a -list-of-the-differences-between-two-javascript-object-graphs)和[***兩個對象之間的泛型深度差異***](http://stackoverflow.com/questions/8572826/generic-deep -diff-之間-雙對象)。請在發佈之前搜索該網站。 –

+0

1.我做到了。大約15分鐘。 2.請在得出結論之前閱讀我的問題。我不想要差異。我想要B的一個子集。基本上B減去A和B的共同值。也許我需要進一步澄清它? –

回答

1

var array1 = { 
 
    "firstName": "John", 
 
    "lastName": "Doe", 
 
    "nickname": "Johnny", 
 
    "location": { 
 
     "latitude": 1.0, 
 
     "longitude": 1.0 
 
    }, 
 
    "email": "[email protected]" 
 
} 
 

 
var array2 = { 
 
    "firstName": "John", 
 
    "lastName": "Doe", 
 
    "nickname": "John-Boy", 
 
    "location": { 
 
     "latitude": 1.0, 
 
     "longitude": 2.0 
 
    }, 
 
    "email": "[email protected]" 
 
} 
 

 
var currentString; 
 
\t \t var resultObject = {}; 
 
\t function loopOverObject(sourceObj, targetObj,innerObjName) { 
 
\t \t for (var key in sourceObj) { 
 
\t \t \t currentString = null; 
 
\t \t \t if (sourceObj.hasOwnProperty(key)) { 
 
\t \t \t \t 
 
\t \t \t \t if (typeof sourceObj[key] === "object") { 
 
\t \t \t \t \t loopOverObject(sourceObj[key],targetObj,key); 
 
\t \t \t \t \t break; 
 
\t \t \t \t } 
 
\t \t \t \t else { 
 
\t \t \t \t \t if (sourceObj[key] !== targetObj[key]) { 
 
\t \t \t \t \t \t if (innerObjName) { 
 
\t \t \t \t \t \t \t if (resultObject[innerObjName]) { 
 
\t \t \t \t \t \t \t \t resultObject[innerObjName][key] = sourceObj[key]; 
 
\t \t \t \t \t \t \t } 
 
\t \t \t \t \t \t \t else { 
 
\t \t \t \t \t \t \t \t resultObject[innerObjName] = JSON.parse('{"'+key+'": '+sourceObj[key]+'}'); 
 
\t \t \t \t \t \t \t } 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t \t else { 
 
\t \t \t \t \t \t \t currentString = sourceObj[key]; 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t } 
 
\t \t \t \t } 
 
\t \t \t \t if (currentString) { 
 
\t \t \t \t \t resultObject[key]=currentString; 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t } 
 
\t \t return resultObject; 
 
\t }; 
 
\t var result = loopOverObject(array2,array1); 
 
\t 
 
var resultDiv = document.getElementById("result") 
 
resultDiv.innerHTML = JSON.stringify(result);
<div id="result"></div>

0

你可以通過獲取不同的列表,並使用它們來構建適當的新對象使用差異模塊像odiff

var diff = odiff(A,B) 
var result= {} 
for(var n=0; n<diff.length; n++) { 
    var key = diff.path[0] // the top-level key 
    result[key] = B[key] 
} 

那麼你得到的結果我認爲你想要的 - 那就是如果你只關心獲取已經改變的頂級域。