繁体   English   中英

删除基于另一个对象的属性

[英]removing the properties based on another object

基于userAccessData对象,我需要过滤属性,以便字段中存在的任何属性都将存在于updatedValue中,我能够做到。

还有一件事,如果输出是一个空对象,则不需要该对象也要删除该对象

我试过下面的代码。 它的工作,但有什么更好的方法,我应该如何删除空对象

 const userAccessData = { forms: { student: [ { studentDetails: ['read', 'create', 'update'], }, ], class: [ { classDetails: ['read', 'create', 'update'], }, { classSecondaryDetails: ['read', 'create', 'update'], }, ], school: [ { schoolContact: ['read', 'create', 'update'], }, { schoolAddress: ['read'], }, { schoolBasicDetails: ['read', 'create'], }, { schoolLocationDetails: ['read', 'create', 'update'], }, ], }, fields: { school: { schoolAddress: [ { isAddress: ['read', 'update'], }, ], schoolLocationDetails: [ { status: ['read'], }, ], schoolContact: [ { contactAddress: ['read'], }, { contactStatus: ['read', 'create'], }, ], }, student: { studentDetails: [ { isAvailable: ['read'], }, ], }, class: { classDetails: [ { classId: ['read', 'create', 'update'], }, ], }, }, }; let updatedValue = { values: { schoolContact: {}, schoolLocationDetails: { status: '123', }, schoolAddress: { isAddress: 'yes', status: 'no' }, } }; const updateDetailsWithAccess = (updatedData, accessListData, selectedSection) => { const accessForms = Object.keys(accessListData[selectedSection]); Object.keys(updatedData.values).forEach((o) => { if (accessForms.indexOf(o) === -1) { delete updatedData.values[o]; } }); Object.keys(updatedData.values).forEach((o) => { const accessFormsForFields = accessListData[selectedSection][o].map(field => Object.keys(field)[0]) Object.keys(updatedData.values[o]).forEach((field) => { if (accessFormsForFields.indexOf(field) === -1) { delete updatedData.values[o][field]; } }); }); return updatedData; }; console.log(updateDetailsWithAccess(updatedValue, userAccessData.fields, 'school')) 

预期产量

{
  "values": {
    "schoolLocationDetails": {
      "status": "123"
    },
    "schoolAddress": {
      "isAddress": "yes"
    }
  }
}

您可以尝试递归删除空对象。 这是我的代码。

 const userAccessData = { forms: { student: [{ studentDetails: ['read', 'create', 'update'], }, ], class: [{ classDetails: ['read', 'create', 'update'], }, { classSecondaryDetails: ['read', 'create', 'update'], }, ], school: [{ schoolContact: ['read', 'create', 'update'], }, { schoolAddress: ['read'], }, { schoolBasicDetails: ['read', 'create'], }, { schoolLocationDetails: ['read', 'create', 'update'], }, ], }, fields: { school: { schoolAddress: [{ isAddress: ['read', 'update'], }, ], schoolLocationDetails: [{ status: ['read'], }, ], schoolContact: [{ contactAddress: ['read'], }, { contactStatus: ['read', 'create'], }, ], }, student: { studentDetails: [{ isAvailable: ['read'], }, ], }, class: { classDetails: [{ classId: ['read', 'create', 'update'], }, ], }, }, }; let updatedValue = { values: { schoolContact: {}, schoolLocationDetails: { status: '123', }, schoolAddress: { isAddress: 'yes', status: 'no' }, } }; function filterObject(obj) { Object.keys(obj).forEach((el) => { if (typeof obj[el] === 'object') filterObject(obj[el]); }); Object.keys(obj).forEach((el) => { if (Object.keys(obj[el]).length === 0) { delete obj[el]; } }); return obj; } const updateDetailsWithAccess = (updatedData, accessListData, selectedSection) => { const accessForms = Object.keys(accessListData[selectedSection]); Object.keys(updatedData.values).forEach((o) => { if (accessForms.indexOf(o) === -1) { delete updatedData.values[o]; } }); Object.keys(updatedData.values).forEach((o) => { const accessFormsForFields = accessListData[selectedSection][o].map(field => Object .keys( field)[0]) Object.keys(updatedData.values[o]).forEach((field) => { if (accessFormsForFields.indexOf(field) === -1) { delete updatedData.values[o][field]; } }); }); return filterObject(updatedData); }; console.log(updateDetailsWithAccess(updatedValue, userAccessData.fields, 'school')) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM