[英]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.