Working with Array.filter () in Javascript

1

Hello! I'm trying to generate a vector with only one "result". Basically I make a query in the mongo that returns me all the consultations done.

I want to generate an array containing only the completed queries, which in the database are stored as status = 'F'. I'll use this array to generate a graph.

In my view I opened a script tag and put the following:

 var teste = [];

    teste.filter(function (){
        if(status == 'F'){

        }
    });

And that's where my question comes from, in that function () do I have to pass an object? (In case this object would be the mongo's collection or something I imagine). And if the status is F, I add in the array, if not, not.

Is it more or less how does the filter work or am I wrong?

    
asked by anonymous 31.07.2018 / 17:20

1 answer

3

filter gets a callback , that is, a function that receives as one of the arguments the elements contained in the array .

So I understand you have a array[{}] of objects, as represented in the variable dados .

In practice:

let dados = [{
        'id': 1,
        'status': 'F'
    },
    {
        'id': 2,
        'status': 'M'
    },
    {
        'id': 3,
        'status': 'F'
    }
]


let novoArray = dados.filter(finalizados);

console.log(novoArray);

function finalizados(value) {
    return value.status === 'F';
}
    
31.07.2018 / 17:37