Saya punya dua array objek. Saya ingin memfilter data berdasarkan PermissionObj.

Ini berasal dari database. Berikut adalah array dari sub-array di permissionObj.

const PermissionObj = {
  permission: [
    {
      books: [
        {
          label: "Can View",
          value: "can_view"
        }
      ]
    },
    {
      Journals: [
        {
          label: "Can View",
          value: "can_view"
        },
        {
          label: "Can Create",
          value: "can_create"
        }
      ]
    },
    {
      deal: [
        {
          label: "Can update",
          value: "can_update"
        },
        {
          label: "Can delete",
          value: "can_delete"
        }
      ]
    }
  ]
}

Ini adalah data statis. Saya ingin membandingkan data ini berdasarkan PermissionObj.

const data = [
  {
    label: "books",
    value: "can_view"
  },
  {
    label: "deal",
    content: [
      {
        value: "can_update"
      },
      {
        value: "can_delete"
      },
      { value: "can_view" }
    ]
  },
  {
    label: "Articles"
  },
  {
    label: "Journals",
    content: [
      {
        value: "can_create"
      },
      {
        value: "can_view"
      },
      {
        value: "can_delete"
      },
      {
        value: "can_edit"
      }
    ]
  }
]

Saya mencoba memfilter array data objek berdasarkan array objek PermissionObj. ini kode percobaan saya.

const permKeys = PermissionObj.permission.flatMap(item => Object.keys(item));
const filteredData = data.filter(({ label }) => permKeys.includes(label));
console.log(filteredData);

Inilah masalah saya, yang saya hadapi adalah saya tidak ingin mendapatkan can_edit, can_delete jika tidak cocok dengan objek izin di jurnal. Di objek izin saya, Tidak ada can_edit dan can_delete di jurnal.

Output saya yang diterima adalah format ini:

const data = [
  {
    label: "books",
    value: "can_view"
  },
  {
    label: "deal",
    content: [
      {
        value: "can_update"
      },
      {
        value: "can_delete"
      }
    ]
  },
  {
    label: "Journals",
    content: [
      {
        value: "can_create"
      },
      {
        value: "can_view"
      }
    ]
  }
]
1
sujon 24 Maret 2020, 15:28

1 menjawab

Jawaban Terbaik

Dimungkinkan untuk menggunakan metode reduce dan menerapkan logika untuk memutuskan data apa yang harus didorong:

const result = data.reduce((a, c) => {
   let filterObj = PermissionObj.permission.find(f => f[c.label]);
   if (filterObj) {
      if (c.value) {
         a.push(c);
      }
      if (c.content) {
         c.content = c.content.filter(f => filterObj[c.label]
             .some(s => s.value.toLowerCase() == f.value.toLowerCase()));
         a.push(c);
      }
   }
   return a;
},[])

Sebuah contoh:

const PermissionObj = {
   permission: [
   {
      "books": [
         {
            "label": "Can View",
            "value": "can_view"
         }
      ]
   },
   {
      "Journals": [
         {
            "label": "Can View",
            "value": "can_view"
         },
         {
            "label": "Can Create",
            "value": "can_create"
         }
      ]
   },
   {
      "deal": [
         {
            "label": "Can update",
            "value": "can_update"
         },
         {
            "label": "Can delete",
            "value": "can_delete"
         }
      ]
   }
 ]
};

const data = [
   {
      label: "books",
      value: "can_view"
   },
   {
      label: "deal",
      content: [
         {
         value: "can_update"
         },
         {
            value: "can_delete"
         },
         {
            value:"can_view"
         }
      ]
   },
   {
      label: "Articles",
   },
   {
      label: "Journals",
      content: [
         {
            value: "can_create"
         },
         {
            value: "can_view"
         },
         {
            value: "can_delete"
         },
         {
            value: "can_edit"
         }
      ]
   }
];


const result = data.reduce((a, c) => {
   let filterObj = PermissionObj.permission.find(f => f[c.label]);
   if (filterObj) {
      if (c.value) {
         a.push(c);
      }
      if (c.content) {
         c.content = c.content.filter(f => filterObj[c.label].some(s => s.value.toLowerCase() == f.value.toLowerCase()));
         a.push(c);
      }
   }
   return a;
},[])
console.log(result);
2
StepUp 24 Maret 2020, 13:14