I need to pass a parameter to an external function that I want to create for a filter
array, but I do not know how to do it. Example: This code already works:
const myArray = [
{ "name": "nome1" },
{ "name": "nome2" },
{ "name": "nome3" },
{ "name": "nome4" },
{ "name": "nome5" }
];
let qParam = myRequest.query.myParam;
// Retorna o item do array cujo campo "name" corresponde a "qParam"
const user = myArray.filter(u => qParam === u.name)[0];
Now I want to do something like this below, but it does not work because I can not pass qParam
per parameter and this variable is also out of scope accessible by myFunction
. How do I resolve this:
function myFunction(value, qParam) {
return value === qParam;
}
const user = myArray.filter(myFunction);