0

I have an array which looks like :

const tilesToShow = ['student','teacher', 'marks', 'presenty']

const response = ['student','teacher', 'internalMarks','RollNumber' 'marks','presenty']

Now, Here I want to show user only the tiles that are present in the tilesToShow array, but need to check the api response as well.

So, I have to filter the reponse, means remove the tiles from the response which are not present in the tilesToShow array. and then iterate the filtered array.

So How can I do this ?

Thanks.

ganesh kaspate
  • 1
  • 9
  • 41
  • 88

4 Answers4

3

You can use filter and includes array method for get your output, hope this will help to you

const tilesToShow = ['student','teacher', 'marks', 'presenty'];

const response = ['student','teacher', 'internalMarks','RollNumber', 'marks','presenty']

console.log(response.filter((e)=> tilesToShow.includes(e)))
Mr.Developer
  • 489
  • 2
  • 8
2
const response = ['student','teacher', 'internalMarks','RollNumber' 'marks','presenty']
var filteredArray = response.filter(function(item) {
    return tilesToShow.indexOf(item) !== -1;
});
shanfeng
  • 503
  • 2
  • 14
Mercury
  • 7,430
  • 3
  • 42
  • 54
0

This snippet will do the work for you.

const tilesToShow = ['student','teacher', 'marks', 'presenty']

const response = ['student','teacher', 'internalMarks','RollNumber' 'marks','presenty']

const filteredResponse = response.filter((item) => tilesToShow.includes(item))
Talha Fayyaz
  • 481
  • 2
  • 9
0

You could use @Mr.Developer suggestion but keep in mind if datasets are big it's worth to consider using Set.has instead of ``Array.includes` due to performance. But for a few values it's not that important.

Krulig
  • 11
  • 1