How can I filter two values in my object array?

Asked

Viewed 90 times

4

Hello! I’m having trouble understanding how I can filter the answer I get from my database, its logic is like this:

I receive an array of objects from which it has the values of use, useridRequerent and useridRetailed, the first value is only to know what situation is that bond of friendship, what matters in this situation is what occurs between the two values.

inserir a descrição da imagem aqui

The user who sends a friend request to another user becomes the Claimant and the user who accepts this friend request is the Defendant. In this case above, the user who is logged in to my system has his id = 6, therefore he is the Plaintiff of the first two relations and the Respondent of the third relation since his number can be found in these three values shown above.

So my goal is to filter the ids unlike the one my user id belongs to, ie if it is id equal to 6, for the indices of 0 and 1, I want to rescue only the useridRedear of which are 2 and 4, but at the same time I want to redeem the value of useridRequerent, since in the third part of this array, id 6 user is the useridRequeried, so it would come along the value of 1.

The first coherent attempt on my head that I made was to condition in a loop of .filter() with an id include on one of the two values in this way:

inserir a descrição da imagem aqui

But the result is always incomplete:

inserir a descrição da imagem aqui

The same is true when I step one && in the condition with useridRequeried, I have used other ties as .map() or .Every() with almost equal conditions in this response array and I still haven’t got the result I’m really waiting for, which way should I do so I can filter through this logic above?

1 answer

1


If I understand correctly, you need to redeem the object that includes the ID you want, regardless of whether you are required or requesting to filter the other ID related to it.

You can try something like:

function filterByRelatedId(id, array) {
    return array.filter(objeto =>  {
        let requerente = objeto.useridRequerente === id;
        let requerido = objeto.useridRequerido === id;
        if (objeto.useridrelacao === 2 && (requerente || requerido)){
            return objeto;
        }
    });
}

Or if you want to redeem only the Ids (I believe that is not the case, because it would not differentiate required from applicant):

function getOnlyIdsByFilteringRelatedId(id, array) {
    let arrayToReturn = [];
    let filteredArray = array.map(objeto =>  {
        let requerente = objeto.useridRequerente === id;
        let requerido = objeto.useridRequerido === id;
        if (objeto.useridrelacao === 2 && (requerente || requerido)){
            return requerido ? objeto.useridRequerente : objeto.useridRequerido;
        }
    });

    for (let objeto of filteredArray) {
        // Check for other properties here if wanted
        if (objeto) {
            arrayToReturn.push(objeto);
        }
    }

    return arrayToReturn;  
}

I wrote the functions assuming a user can’t be requested and applicant at the same time.

  • It’s just the second function I’ve seen that I really need, however it returns to me Undefined together with the values I need, there is how to return only the values without these undefineds?

  • @Rafaelsoares The answer has been edited to support your question. Just a second iteration in the same function but in the array filtered, by inserting whatever has value (not null, undefined etc.) in a second array that will return from the function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.