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

时间:2019-07-04 05:57:28

标签: javascript

基于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"
    }
  }
}

1 个答案:

答案 0 :(得分:0)

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

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