我需要根据用户输入的内容动态更新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
值后,如何返回新的过滤后的数组?
答案 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);
});
})
}