function isEqual(obj1, obj2) { const keys1 = Object.keys(obj1); const keys2 = Object.keys(obj2); if (keys1.length !== keys2.length) return false; for (const key of keys1) { // if (Array.isArray(obj1[key])) { // compareArrays(obj1[key], obj2[key]) // } else { if (obj1[key] !== obj2[key]) { console.log(key) } // } ; } return true; } function compareArrays(oldArray, newArray) { const oldMap = new Map(oldArray.map(item => [item.id, item])); const newMap = new Map(newArray.map(item => [item.id, item])); const added = newArray.filter(item => !oldMap.has(item.id)); const deleted = oldArray.filter(item => !newMap.has(item.id)); const modified = []; for (const newItem of newArray) { const oldItem = oldMap.get(newItem.id); if (oldItem) { const changes = {}; let hasChange = false; for (const key in newItem) { if (!Object.is(newItem[key], oldItem[key])) { changes[key] = {old: oldItem[key], new: newItem[key]}; hasChange = true; } } if (hasChange) { modified.push({id: newItem.id, changes}); } } } console.log('新增:', added); console.log('删除:', deleted); console.log('修改:', modified); return {added, deleted, modified}; } obj1 =[ { "swi": 1920 }, { "wiw": 804 }, { "pha": 0 }, { "asw": 1920 }, { "nap": "Gecko" }, { "ua": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36" }, { "ibr": 0 }, { "dau": 0 }, { "ash": 1040 }, { "nps": "20030107" }, { "she": 1080 }, { "tsd": 0 }, { "hz1": 429998 }, { "ucs": "8106" }, { "ran": "0.17157431385" }, { "xag": 12147 }, { "hal": 873811374209 }, { "nal": "zh-CN" }, { "wih": 919 }, { "npl": 5 }, { "wow": 1920 }, { "wdr": 0 }, { "adp": "cpen:0,i1:0,dm:0,cwen:0,non:1,opc:0,fc:0,sc:0,wrc:1,isc:0,vib:1,bat:1,x11:0,x12:1" } ] obj2 = [ { "asw": 1920 }, { "npl": 5 }, { "nap": "Gecko" }, { "ucs": "8106" }, { "wiw": 0 }, { "swi": 1920 }, { "nal": "zh-CN" }, { "dau": 0 }, { "pha": 0 }, { "wdr": 0 }, { "ua": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36" }, { "adp": "cpen:0,i1:0,dm:0,cwen:0,non:1,opc:0,fc:0,sc:0,wrc:1,isc:0,vib:1,bat:1,x11:0,x12:1" }, { "ibr": 0 }, { "wih": 0 }, { "hz1": 429875 // }, { "xag": 11891 }, { "hal": 873562480929 }, { "ash": 1040 }, { "she": 1080 }, { "tsd": 0 }, { "ran": "0.0113193795" }, { "nps": "20030107" }, { "wow": 1920 } ] isEqual(obj1, obj2)