Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return filtered array once compared to value of object property in second array

I need to dynamically update a table in my AngularJS app, based on what the user inputs. I have an updateCategory function that is invoked by ng-change in the view and should return a new array to vm.portfolioData in the controller.

The el.serviceList provides a property, serviceStatus, which provides a string value, which then needs to be checked if it is included in a object property in a second array, actionStatus:

function updateCategory(obj, position) {
  vm.portfolioData = patents;
  vm.portfolioData = vm.portfolioData.filter(function(el){
     return el.serviceList.filter(function(item){
        //item.serviceStatus: "Epct available"
        //NEED TO RETURN EL IF STATUS IS CONTAINED IN SECOND ARRAY
     }
  })
}

Second array

actionStatus: [
   {name: 'Epct available'},
   {name: 'Epct saved'}, 
   {name: 'Form 1200 saved'}, 
   {name: 'Epct rejected'}, 
   {name: 'Show price'}, 
   {name: 'Open for Renewal'}, 
   {name: 'Form 1200 generating'}
 ]

Question

How do I return the new filtered array once I have checked if the serviceStatus value is contained in any of the objects property name in the second array?

like image 670
Patrick McDermott Avatar asked Feb 16 '26 04:02

Patrick McDermott


1 Answers

Try this:

const serviceList = [
    {serviceStatus: 'Epct available'},
    {serviceStatus: 'Epct not available'},
];

const actionStatus = [
    {name: 'Epct available'},
    {name: 'Epct saved'},
    {name: 'Form 1200 saved'},
    {name: 'Epct rejected'},
    {name: 'Show price'},
    {name: 'Open for Renewal'},
    {name: 'Form 1200 generating'}
];

const actionStatusStrings = actionStatus.map(item => item.name);

const filteredArray = serviceList.filter(item => {
    return actionStatusStrings.includes(item.serviceStatus);
});

Regarding your case:

function updateCategory(obj, position) {
  vm.portfolioData = patents;

  const actionStatusStrings = actionStatus.map(item => item.name);

  vm.portfolioData = vm.portfolioData.filter(el => {
    return el.serviceList.find(item => {
        return actionStatusStrings.includes(item.serviceStatus);
    });
  })
}
like image 165
Ihor Sakailiuk Avatar answered Feb 17 '26 17:02

Ihor Sakailiuk