Home > Enterprise >  Check the array of element in another array of set of elements in javascript
Check the array of element in another array of set of elements in javascript

Time:10-06

Let openstatus = ["Open","Created","Checked"]

Let obj = [
  { id: 00023434, Authore: tester, status: Approval }, 
  { id: 0002578, Authore: apitester, status: Open }, 
  { id: 0002847, Authore: usertester, status: Created },
{ id: 0002847, Authore: usertester, status: Draft },
]

Example : Let status = "Draft" obj.filter((e1)

return e1.status.includes(status) );

from above one output will be below output:[id: 0002847, Authore: usertester, status: Draft]

When the status has all list of values ["Open","Created","Checked"] then how to filter it

I need to check the above openstatus of list of array status values match to check from obj array and to find to give last 2 records. can I know simple step to resolve this issue.As I am new to javascript

CodePudding user response:

You can just filter using the condition that the openstatus array includes the element's status.

let openstatus = ['Open', 'Created', 'Checked'];

let obj = [
  { id: 00023434, Authore: 'tester', status: 'Approval' }, 
  { id: 0002578, Authore: 'apitester', status: 'Open' }, 
  { id: 0002847, Authore: 'usertester', status: 'Created' },
  { id: 0002847, Authore: 'usertester', status: 'Draft' },
];

console.log(obj.filter(el => openstatus.includes(el.status)));

CodePudding user response:

Check out filter method. Give the documentation a read - filter()

  • Related