I'm working with express.js and wondering how this function returns all the pertinent data about an animal. The query is searching an animal by name, it returns all of the info about the animal but I don't know how the function works.
function filterByQuery(query, animalsArray) {
let filteredResults = animalsArray;
if (query.diet) {
filteredResults = filteredResults.filter(animal => animal.diet === query.diet);
}
if (query.species) {
filteredResults = filteredResults.filter(animal => animal.species === query.species);
}
if (query.name) {
filteredResults = filteredResults.filter(animal => animal.name === query.name)
}
return filteredResults;
}