兩個json的差異直接印出來不太好找,想要用程式比對只看差異的部分
Learn More →
https://codepen.io/not0000/pen/oNePJXm
const obj1 = {
"id": 50,
"name": "AIK",
"description": "Hejja!",
"tasks": [{ 'foo': 'bar' }],
"another1":"you can't see me"
};
const obj2 = {
"id": 50,
"name": "DIFF",
"description": "Hejja!",
"tasks": [{ 'foo': 'zaz' }],
"another2":"by john cena"
};
function diff(obj1, obj2) {
const result = {};
if (Object.is(obj1, obj2)) {
return undefined;
}
if (!obj2 || typeof obj2 !== 'object') {
return obj2;
}
Object.keys(obj1 || {}).concat(Object.keys(obj2 || {})).forEach(key => {
if (obj2[key] !== obj1[key] && !Object.is(obj1[key], obj2[key])) {
result[key] = obj2[key];
}
if (typeof obj2[key] === 'object' && typeof obj1[key] === 'object') {
const value = diff(obj1[key], obj2[key]);
if (value !== undefined) {
result[key] = value;
}
}
});
return result;
}
console.log(JSON.stringify(diff(obj1, obj2), null, 2))
如果是來源有的屬性,但比較對象沒有,那不會顯示出來的,請見範例的 another1 和 another2 的情形
https://www.cloudskillsboost.google/paths/118?locale=zh_TW
May 7, 2025IIS Crypto 是一個免費軟體,可以利用圖形介面關閉舊的加密協定或是不安全的加密演算法
Feb 20, 2025如果擔心版權問題,請見 素材使用注意事項
Feb 12, 2025MVC提供方便的Bundling功能,可以把css、js打包壓縮,只要兩個步驟就完成了
Jan 4, 2025or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up