Filter with 2 Object Array

Asked

Viewed 33 times

-1

I need to filter using 2 object array so that the first does not have the same information as the second ex:

valorA = [{nome: 'rafael', valor: 29}, {nome: 'diego', value: 21},{nome: 'tiago', value: 12}]

valorB = [{nome: 'rafael', valor: 29}, {nome: 'diego', value: 21}]


// valorA tem que vir somente com tiago, 
valorA = valorA.filter(valor => {
     return valor.nome != valorB[index].nome
})

I’m trying something kind of like this but don’t go out, someone to help me?

1 answer

0

To do this you need to check for each element of A if the same exists within B, you can do it in several ways, a simple would be using the Find Index, basically for each value in A you search for the corresponding index of the same in B, if the condition of the filter will be satisfied and the new array will have different values of A and B.

const valorA = [{nome: 'rafael', valor: 29}, {nome: 'diego', value: 21},{nome: 'tiago', value: 12}]
const valorB = [{nome: 'rafael', valor: 29}, {nome: 'diego', value: 21}]


const valorC = valorA.filter(valor => valorB.findIndex(vB => vB.nome === valor.nome) === -1);

console.log(valorC)

Browser other questions tagged

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