如何判断一个对象中的属性和改属性对应的Old值完全相同。

如何判断一个对象中的属性和改属性对应的Old值完全相同。其属性中包含有数组。

{

"nameEn": "zhangsan",
"nameEnOld": "zhangsan",
"nameZh": "张三",
"nameZhOld": "张三",
"nation": "中国",
"nationOld": "中国",
"nativePlace": "上海",
"nativePlaceOld": "上海",
"officeLocation": "合肥",
"officeLocationOld": "合肥",
"jobExperienceList": [
    {
        "attesters": "",
        "beginDate": "2019-06-01 00:00:00",
        "companyName": "某公司",
        "contactPhone": "18900009898",
        "contactSceoId": "",
        "createdEmpId": "00000001",
        "createdTime": "2022-07-05 17:49:53",
        "endDate": "2020-02-01 00:00:00",
        "id": "a5684b5243904ea0ba73407014c6ea7b",
        "isDeleted": false,
        "jobContent": "",
        "modifiedEmpId": "",
        "modifiedTime": null,
        "pid": "5bcbf4231fd048c3a0b11da79aec78b8",
        "position": "哈哈哈哈哈",
        "relationId": "FR0001"
    }
],
"jobExperienceListOld": [
    {
        "attesters": "",
        "beginDate": "2019-06-01 00:00:00",
        "companyName": "某公司",
        "contactPhone": "18900009898",
        "contactSceoId": "",
        "createdEmpId": "00000001",
        "createdTime": "2022-07-05 17:49:53",
        "endDate": "2020-02-01 00:00:00",
        "id": "a5684b5243904ea0ba73407014c6ea7b",
        "isDeleted": false,
        "jobContent": "",
        "modifiedEmpId": "",
        "modifiedTime": null,
        "pid": "5bcbf4231fd048c3a0b11da79aec78b8",
        "position": "哈哈哈哈哈",
        "relationId": "FR0001"
    }
]

}

阅读 1.1k
2 个回答

可以参考一些 diff 算法,基础算法为 最短编辑距离

分别取出来对比

let obj = {

"nameEn": "zhangsan",
"nameEnOld": "zhangsan",
"nameZh": "张三",
"nameZhOld": "张三",
"nation": "中国",
"nationOld": "中国",
"nativePlace": "上海",
"nativePlaceOld": "上海",
"officeLocation": "合肥",
"officeLocationOld": "合肥",
"jobExperienceList": [
    {
        "attesters": "",
        "beginDate": "2019-06-01 00:00:00",
        "companyName": "某公司",
        "contactPhone": "18900009898",
        "contactSceoId": "",
        "createdEmpId": "00000001",
        "createdTime": "2022-07-05 17:49:53",
        "endDate": "2020-02-01 00:00:00",
        "id": "a5684b5243904ea0ba73407014c6ea7b",
        "isDeleted": false,
        "jobContent": "",
        "modifiedEmpId": "",
        "modifiedTime": null,
        "pid": "5bcbf4231fd048c3a0b11da79aec78b8",
        "position": "哈哈哈哈哈",
        "relationId": "FR0001"
    }
],
"jobExperienceListOld": [
    {
        "attesters": "",
        "beginDate": "2019-06-01 00:00:00",
        "companyName": "某公司",
        "contactPhone": "18900009898",
        "contactSceoId": "",
        "createdEmpId": "00000001",
        "createdTime": "2022-07-05 17:49:53",
        "endDate": "2020-02-01 00:00:00",
        "id": "a5684b5243904ea0ba73407014c6ea7b",
        "isDeleted": false,
        "jobContent": "",
        "modifiedEmpId": "",
        "modifiedTime": null,
        "pid": "5bcbf4231fd048c3a0b11da79aec78b8",
        "position": "哈哈哈哈哈",
        "relationId": "FR0001"
    }
]
}
let reg = /Old$/;// 判断对象键是否以Old结尾
let {newObj, oldObj} = Object.entries(obj).reduce((acc,[key,val])=>{
    if(reg.test(key)){
        acc.oldObj[key.replace(reg,'')] = val;
    } else{
        acc.newObj[key] = val;
    }
    return acc;
},{newObj:{}, oldObj:{}})

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