Home > Enterprise >  how to compare array of objects with object key value, if matches retain if not filter out using ang
how to compare array of objects with object key value, if matches retain if not filter out using ang

Time:08-12

i have array of objects and one object, here i need to compare array of objects with the object key , if object key matches with array of object param, we need to retain that and remove the objects whose key doesnt matches with array param.

data:

array1=[{displayName: 'Chase Id', param: 'chaseId', icon: 'fa fa-sort'},
{displayName: 'Last Modified By', param: 'lastModifiedBy', icon: 'fa fa-sort'},
{displayName: 'Last Modified Date', param: 'lastModifiedDate', icon: 'fa fa-sort'},
 {displayName: 'Imported Chase Id', param: 'importedChaseId', icon: 'fa fa-sort'},
 {displayName: 'Chase Status Name', param: 'chaseStatusName', icon: 'fa fa-sort'},
{displayName: 'Pend Reason', param: 'pendReason', icon: 'fa fa-sort'},
{displayName: 'Active', param: 'active', icon: 'fa fa-sort'},
{displayName: 'Last Contacted Date', param: 'lastContactedDate', icon: 'fa fa-sort'},
 {displayName: 'Ready To Review Date', param: 'readyToReviewDate', icon: 'fa fa-sort'},
 {displayName: 'Scheduled Date', param: 'scheduledDate', icon: 'fa fa-sort'},
 {displayName: 'Release Pend To Client', param: 'releasePendToClient', icon: 'fa fa-sort'}]



obj ={
chaseId: 419,
chaseStatusName: "Scheduled",
id: 42,
importedChaseId: "123478",
lastModifiedBy: "adminuser",
lastModifiedDate: "2022-08-12T12:43:17.792023",
pendReason: null,
type: "CHASE_STATUS"
}

CodePudding user response:

const keys = Object.keys(obj);     
array1.filter((item) => keys.includes(item.param));

Something like that?

CodePudding user response:

const array1 = [{
    displayName: 'Chase Id',
    param: 'chaseId',
    icon: 'fa fa-sort'
  },
  {
    displayName: 'Last Modified By',
    param: 'lastModifiedBy',
    icon: 'fa fa-sort'
  },
  {
    displayName: 'Last Modified Date',
    param: 'lastModifiedDate',
    icon: 'fa fa-sort'
  },
  {
    displayName: 'Imported Chase Id',
    param: 'importedChaseId',
    icon: 'fa fa-sort'
  },
  {
    displayName: 'Chase Status Name',
    param: 'chaseStatusName',
    icon: 'fa fa-sort'
  },
  {
    displayName: 'Pend Reason',
    param: 'pendReason',
    icon: 'fa fa-sort'
  },
  {
    displayName: 'Active',
    param: 'active',
    icon: 'fa fa-sort'
  },
  {
    displayName: 'Last Contacted Date',
    param: 'lastContactedDate',
    icon: 'fa fa-sort'
  },
  {
    displayName: 'Ready To Review Date',
    param: 'readyToReviewDate',
    icon: 'fa fa-sort'
  },
  {
    displayName: 'Scheduled Date',
    param: 'scheduledDate',
    icon: 'fa fa-sort'
  },
  {
    displayName: 'Release Pend To Client',
    param: 'releasePendToClient',
    icon: 'fa fa-sort'
  }
]

const obj = {
  chaseId: 419,
  chaseStatusName: "Scheduled",
  id: 42,
  importedChaseId: "123478",
  lastModifiedBy: "adminuser",
  lastModifiedDate: "2022-08-12T12:43:17.792023",
  pendReason: null,
  type: "CHASE_STATUS"
}

console.log(array1.filter(x => {
  let output = false;
  for (let key in obj) {
    if (!output) {
      output = !!(key === x.param);
    }
  }
  return output;
}));

  • Related