1
I have the following two objects in Angularjs:
$scope.listaDoCarrinho = [0: {
id: "55",
setor: "alimento",
foto: "Produtos/Produto (55).jpg",
descr: "Espaguete Renata",
de: 15,
…
}
1: {
id: "1000",
setor: "biscoitos",
foto: "Produtos/Produto (1000).jpg",
descr: "Biscoito Pit-Stop",
de: 3,
…
}
2: {
id: "3",
setor: "higiene",
foto: "Produtos/Produto (3).jpg",
descr: "Bronzeador 200ml",
de: 15,
…
}
];
$scope.listademercadoria1 = [0: {
id: "55",
setor: "alimento",
foto: "Produtos/Produto (55).jpg",
descr: "Espaguete Renata",
de: 15,
…
}
1: {
id: "1000",
setor: "biscoitos",
foto: "Produtos/Produto (1000).jpg",
descr: "Biscoito Pit-Stop",
de: 3,
…
}
2: {
id: "197",
setor: "sobremesa",
foto: "Produtos/Produto (197).jpg",
descr: "Nutella",
de: 10,
…
}
4: {
id: "1",
setor: "higiene",
foto: "Produtos/Produto (1).jpg",
descr: "Bronzeador",
de: 200,
…
}
];
I need a command . filter that returns me $scope.listademercadoria1 - $scope.listaDoCarrinho
(Variable lister1 minus the variable listDoCart), filtered by id
.
After much research, I arrived at these conclusions below, however fruitless:
Attempt 1:
$scope.listaMercadoriaNova = $scope.listademercadoria1.filter(
$scope.listademercadoria1.id = $scope.listaDoCarrinho.id);
Attempt 2:
$scope.listaMercadoriaNova = $scope.listademercadoria1.filter(
function() {
return $scope.listademercadoria1.id != $scope.listaDoCarrinho.id;
});
$scope.listademercadoria1 = $scope.listaMercadoriaNova;
I think it’s explained well, anything I can improve the explanation, every comment is welcome.
Thank you Sergio. In your example it really works, but when I use this code here, the system returns in the console the same arrays contained in $Scope.listDoCarrinho. Do you know what might be happening? It will be because it is in Angularjs that I am programming?
– Gymo
Could you tell me the reverse of that? In case I only need to keep what is equal in both?
– Gymo
@Guilhermesilvadeoliveira if you do
$scope.listaMercadoriaNova = filtrados
that I have in my example does not work?– Sergio
@Guilhermesilvadeoliveira I may have done it backwards, but maybe it’s
.filter(obj => !$scope...etc
with!
that you want, to give only items that can not find.– Sergio
It worked! Very grateful for the help, I will pass the same knowledge to others, you can leave!
– Gymo