I have an array of objects with some properties. If cod and art already exist in the array, I want to remove the repeated ones.
I created a function for this, but my result is wrong... instead of having 3 lines, I have 2.
Can anyone help me?
.TS
data = [
{
cod: 1.13,
art: 'BCC'
},
{
cod: 1.13,
art: 'BCC'
},
{
cod: 1.13,
art: 'BCC'
},
{
cod: 1.14,
art: 'BCC'
},
{
cod: 1.14,
art: 'BCC'
},
{
cod: 1.13,
art: 'AAA'
},
{
cod: 1.13,
art: 'AAA'
}
];
ngOnInit() {
var filtered = this.data.filter(function(el) {
if (!this[el.cod && el.art]) {
this[el.cod && el.art] = true;
return true;
}
return false;
}, Object.create(null));
console.log(filtered);
}
//Expected Output
// data = [
// {
// cod: 1.13,
// art: 'BCC'
// },
// {
// cod: 1.14,
// art: 'BCC'
// },
// {
// cod: 1.13,
// art: 'AAA'
// }
// ];