简体   繁体   English

如何使用angular6根据字符串数组过滤对象数组

[英]How to filter the array of objects based on array of strings by using angular6

I have 2 arrays of string and 1 array of objects.我有 2 个字符串数组和 1 个对象数组。 I need to filter based on 2 arrays of string.我需要根据 2 个字符串数组进行过滤。 how can I achieve this?我怎样才能做到这一点?

let profissionals = [
{state:'ap',type:'maths',price:200},
{state:'ap',type:'english',price:400},
{state:'ka',type:'social',price:200},
{state:'ts',type:'english',price:200},
{state:'ap',type:'maths',price:500},
{state:'ka',type:'maths',price:600},
{state:'ts',type:'english',price:200},
{state:'kl',type:'english',price:100},
{state:'kl',type:'english',price:300},
{state:'ap',type:'social',price:200},
{state:'gj',type:'english',price:600},
{state:'kl',type:'social',price:600}
]

let typeList=['maths','social'];
let stateList=['ap','ka'];

the output should be like this输出应该是这样的

fliteredlist  = [
{state:'ap',type:'maths',price:200},
{state:'ap',type:'maths',price:500},
{state:'ka',type:'maths',price:600},
{state:'ap',type:'social',price:200},
{state:'ka',type:'social',price:200},
]

You can use Array.filter() :您可以使用Array.filter()

 let profissionals = [ {state:'ap',type:'maths',price:200}, {state:'ap',type:'english',price:400}, {state:'ka',type:'social',price:200}, {state:'ts',type:'english',price:200}, {state:'ap',type:'maths',price:500}, {state:'ka',type:'maths',price:600}, {state:'ts',type:'english',price:200}, {state:'kl',type:'english',price:100}, {state:'kl',type:'english',price:300}, {state:'ap',type:'social',price:200}, {state:'gj',type:'english',price:600}, {state:'kl',type:'social',price:600} ] let typeList=['maths','social']; let stateList=['ap','ka']; let output = profissionals.filter(({type, state}) => (typeList.length === 0 || typeList.includes(type)) && (stateList.length === 0 || stateList.includes(state))); console.log(output);

profissionals.filter(({state, type}) => stateList.includes(state) && typeList.includes(type));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用angular6在基于id值的第一个数组对象中添加第二个数组对象? - How to add second array objects in first array object based on id value using angular6? 如何根据字符串数组过滤 javascript 对象数组 - How to filter an array of javascript objects based on an array of strings 使用$ filter基于Angular控制器中的属性对对象数组进行排序 - Sorting an array of objects based on a property in an Angular controller using $filter 如何根据 Angular 中另一个对象的属性过滤一组对象? - How to filter an array of objects based of attribute from another object in Angular? 如何使用 javascript 中的过滤器根据性别过滤出对象数组? - How to filter out array of objects based on gender using filter in javascript? 如何基于对象数组中的元素使用角度滤镜 - how to use angular filter based on the elements inside array of objects 按字符串数组过滤对象数组 - Filter array of objects by array of strings 基于对象的对象过滤器数组如何存在于另一个对象数组中? - How filter array of objects based on objects exists in another array of objects? Angular6 管道过滤器在大型阵列上运行缓慢 - Angular6 pipe filter run slow on large array 如何使用 Angular 中的过滤器访问对象元素的嵌套数组? - How to access nested array of objects element using filter in Angular?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM