2个数组对比求不一样的索引

"before_data": {
                "id": "2311",
                "user_id": "4",
                "name": "OPPO\u5355\u673a",
                "app_id": "999",
                "package_id": "1881",
                "platform_id": "20027",
                "platform_name": "singleOppo",
                "channel": "",
                "login_url": "",
                "uid_prefix": "singleOppo_",
                "config": null,
                "ext_data": null,
                "open_notify_sync": "0",
                "notify_sync_type": "2",
                "notify_sync_url": null,
                "updated_at": "1503543725",
                "created_at": "1503543725",
                "deleted_at": null
            },
            "after_data": {
                "id": "2311",
                "user_id": "4",
                "name": "OPPO\u5355\u673a",
                "app_id": "999",
                "package_id": "1881",
                "platform_id": "20027",
                "platform_name": "singleOppo",
                "channel": "",
                "login_url": "",
                "uid_prefix": "singleOppo_",
                "config": "{\r\n  \"app_id\": \"\",\r\n  \"app_key\": \"\",\r\n  \"app_secre\": \"\",\r\n  \"public_key\": \"\"\r\n}",
                "ext_data": null,
                "open_notify_sync": "0",
                "notify_sync_type": "2",
                "notify_sync_url": null,
                "updated_at": "1503543725",
                "created_at": "1503543725",
                "deleted_at": null
            }

after_data和before_data 2个对象对比如何知道第几个索引的值不一样或者哪个key不一样?

阅读 3.1k
3 个回答
arr1.findIndex( ( e, i ) => JSON.stringify( e ) !== JSON.stringify( arr2[ i ] ) );
var json1 = {name:"a",age:12};
var json2 = {name:"a",age:15};
var result = Object.keys(json1).filter((v)=>json1[v]!=json2[v]);
console.log(result)//["age"]

合并的话有其他很优雅写法。求diff话,只能遍历。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题