0

I have 2 arrays of string and 1 array of objects. I need to filter based on 2 arrays of string. 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},
]
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
Rupesh
  • 3
  • 6
  • 1
    _"how can I achieve this?"_ -> With some loops. That said... What have you tried so far to solve this on your own? – Andreas Jan 01 '20 at 12:31
  • @Rupesh here is exact solution that you need [filter array from another array](https://stackoverflow.com/a/46894550/9775003) – Sanoj_V Jan 01 '20 at 12:51

2 Answers2

0

You can use 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);
Ankita Kuchhadiya
  • 1,255
  • 6
  • 16
  • Thanks, but if the stateList is Empty array. let typeList=['maths','social']; let stateList=[]; i need to get all maths and social elements – Rupesh Jan 01 '20 at 12:41
  • [{state:'ap',type:'maths',price:200},{state:'ka',type:'social',price:200},{state:'ap',type:'maths',price:500},{state:'ka',type:'maths',price:600},{state:'ap',type:'social',price:200},{state:'kl',type:'social',price:600}] – Rupesh Jan 01 '20 at 12:47
  • @Rupesh I did not get you – Ankita Kuchhadiya Jan 01 '20 at 12:47
  • @Rupesh here is exact solution that you need [filter array from another array](https://stackoverflow.com/a/46894550/9775003) – Sanoj_V Jan 01 '20 at 12:52
  • for example in two arrays one is empty. typeList=['maths','social'], stateList=[]. here typeList have values maths and social but statelist does not have any values. in this condition the output should be. like this [{state:'ap',type:'maths',price:200},{state:'ka',type:'social',price:200},{state:'ap',type:'maths',price:500},{state:'ka',type:'maths',price:600},{state:'ap',type:'social',price:200},{state:'kl',type:'social',price:600}] – Rupesh Jan 01 '20 at 12:54
  • @Rupesh check my updated code. It works as your requirements. – Ankita Kuchhadiya Jan 01 '20 at 13:02
0
profissionals.filter(({state, type}) => stateList.includes(state) && typeList.includes(type));
Maor M
  • 11
  • 3