How to filtering array of objects to another array of objects in javascript? [closed]

拈花ヽ惹草 提交于 2020-04-05 05:17:49

问题


I have got two arrays of objects . I want to filtering data based on PermissionObj.

This is coming form database. Here is arrays of sub-arrays in the 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"
        },
       ]
      }
     ]
    }

this is static data. I want to compare this data based on 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"
   },

  ]
 }
]

I am trying filter data array of object based on PermissionObj array of objects . here is my trying code.

let permission = PermissionObj.permission 

permission.filter(item=>{
  // I am finding data label by using permission label.

 let dataItem = data.find(index=>item[index.label])

 console.log(dataItem)

})

  // the problem is that I want to filtering based on value .

  // if both of value is true , then show the data . 

if PermissionObj value will be matched with data value . then show the data .
My accepted Output would be this format :

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:


First build keys from PermissionObj. Use filter on data array which label includes in keys.

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"
      }
    ]
  },
  {
    label: "Articles"
  },
  {
    label: "Journals",
    content: [
      {
        value: "can_create"
      },
      {
        value: "can_view"
      }
    ]
  }
];

const perm = {};
PermissionObj.permission.forEach(item =>
  Object.entries(item).forEach(([key, values]) => {
    perm[key] = values.map(x => x.value);
  })
);

const updated = data
  .map(item => {
    const newItem = {
      ...item
    };
    if (item.content) {
      newItem.content = item.content.filter(x =>
        perm[item.label].includes(x.value)
      );
    } else if (item.value) {
      newItem.value = perm[item.label].includes(item.value) ? item.value : "";
    }
    return newItem;
  })
  .filter(x => x.content || x.value);

console.log(updated);



回答2:


so you can get keys of the permission array, this would give you an array of keys.since you are getting the keys of an item in an array of objects so the key would always be the first item then you can check each data item with that key

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"
        }
      ]
    },
    {
      label: "Articles"
    },
    {
      label: "Journals",
      content: [
        {
          value: "can_create"
        },
        {
          value: "can_view"
        }
      ]
    }
  ];
  
    let permission = PermissionObj.permission;
    let NewData = [];
    permission.filter(item => {
      let ObjectKeys = Object.keys(item);
      let ObjKey = ObjectKeys[0];
      let DATA = data.find(
        dataItem => dataItem.label.toString() === ObjKey.toString()
      );
      NewData.push(DATA);
    });
    console.log(NewData);


来源:https://stackoverflow.com/questions/60829455/how-to-filtering-array-of-objects-to-another-array-of-objects-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!