0

0

I am trying to filter a javascript array (json array) with the string array, and set it back in itself.

I am using this code`

var statusFilters = component.get("v.statusFilters");
console.log('statusFilters--->'+statusFilters);
var allRecords = JSON.stringify(component.get("v.empWrapperList"));
console.log('allRecords--->'+allRecords);
var filteredRecords = allRecords.filter(rec => rec.Status__c == statusFilters);
console.log(filteredRecords);
component.set("v.empWrapperList",filteredRecords);`

Here statusFilter is a string Array and allRecords is a json array.

Here are the logs from console.

statusFilters--->Paid ClaimsDemo.js:119 allRecords--->[{"Id":"a1V2x000001K29pEAC","Name":"CL-0000004","Member__c":"0032x000004bgAkAAI","Date_of_Service__c":"2020-06-25","Provider__c":"a112x000003VXGEAA4","Status__c":"Void"},{"Id":"a1V2x000001K14OEAS","Name":"CL-0000003","Member__c":"0032x000004bgAkAAI","Billed__c":22,"Date_of_Service__c":"2015-09-15","Provider__c":"a112x000003VXGEAA4","Status__c":"Denied"},{"Id":"a1V2x000001K14JEAS","Name":"CL-0000002","Member__c":"0032x000004bgAkAAI","Billed__c":22,"Date_of_Service__c":"2019-10-16","Provider__c":"a112x000003VXGEAA4","Status__c":"Rejected"},{"Id":"a1V2x000001K14EEAS","Name":"CL-0000001","Member__c":"0032x000004bgAkAAI","Billed__c":22,"Date_of_Service__c":"2020-06-04","Provider__c":"a112x000003VXGEAA4","Status__c":"Paid"}]

Actually it is unable to execute this line var filteredRecords = allRecords.filter(rec => rec.Status__c == statusFilters);

Can you please help.

1 Answer 1

0

There are several ways you could do this, such as:

var filteredRecords = allRecords.filter(
  rec => statusFilters.find(
    status => status === rec.Status__c
  )
);

Or:

var filteredRecords = allRecords.filter(rec => statusFilters.indexOf(rec.Status__c) > -1);

Or:

var filteredRecords = allRecords.filter(
  rec => statusFilters.some(
    status => status === rec.Status__c
  )
);

The main point is that == would require that the selected status is equal to all the filters, which will never be true, even if only one value is selected.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .