| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- 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)
|