将过滤后的数组与第二个数组中的对象属性的值进行一次比较

时间:2018-10-18 08:57:24

标签: javascript arrays angularjs

我需要根据用户输入的内容动态更新AngularJS应用中的表。我有一个updateCategory函数,该函数由视图中的ng-change调用,并且应该向控制器中的vm.portfolioData返回一个新数组。

el.serviceList提供了一个属性serviceStatus,该属性提供了一个字符串值,然后需要检查它是否包含在第二个数组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
     }
  })
}

第二个数组

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'}
 ]

问题

检查第二个数组的任何对象属性serviceStatus中是否包含name值后,如何返回新的过滤后的数组?

1 个答案:

答案 0 :(得分:2)

尝试一下:

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);
});

关于您的情况:

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);
    });
  })
}