El Hombre Sin Nombre
El Hombre Sin Nombre

Reputation: 3092

Angular - Remove duplicates in array with same name element

The problem is simple.

How can i filter this array with the 'alarma' element to get only one

0: {alarma: 1797, nroSerie: "8764368", nombre: "Alarma cable cortado", fechaInicio: 1566194460, fechaFin: 1566311460}
1: {alarma: 1797, nroSerie: "8764368", nombre: "Alarma cable cortado", fechaInicio: 1566311460, fechaFin: 1566311580}
2: {alarma: 1797, nroSerie: "8764368", nombre: "Alarma cable cortado", fechaInicio: 1566311580, fechaFin: null}

Upvotes: 0

Views: 674

Answers (4)

Yevhenii Dovhaniuk
Yevhenii Dovhaniuk

Reputation: 1103

If you want to get rid of duplicates by a given key without using lodash library you can do the following:

For a given array [{a: 5, b: 7}, {a: 5, b: 99}, {a: 6, b: 1}, {a: 6, b: 0}] the expected result can be [{a: 5, b: 99}, {a: 6, b: 0}] (the last values are taken)

to implement this simply:

  1. Create an object having a key as a value of 'a' property:
const array = [{a: 5, b: 7}, {a: 5, b: 99}, {a: 6, b: 1}, {a: 6, b: 0}];
const unique = array.reduce((acc, value) => { 
  acc[value.a] = value; 
  return acc;
}, {});

The unique object will be the following:

{"5":{"a":5,"b":99},"6":{"a":6,"b":0}}
  1. Take the values of this object using:
const result = Object.values(unique);

The result value will be:

[{"a":5,"b":99},{"a":6,"b":0}]

If you want only the first item of a duplicate to be taken change the code to:

const array = [{a: 5, b: 7}, {a: 5, b: 99}, {a: 6, b: 1}, {a: 6, b: 0}];
const unique = array.reduce((acc, value) => { 
  acc[value.a] = acc[value.a] || value; // note the changes at this line
  return acc;
}, {});
const result = Object.values(unique);

The output will be:

[{"a":5,"b":7},{"a":6,"b":1}]

Upvotes: 0

ANSALKHAN K A
ANSALKHAN K A

Reputation: 1

One option is to use the

array.indexOf(obj)

function. If the array has the same element already the indexOf function will return some valid index. before doing the

array.push(obj)

check the

array.indexOf(obj).

function first

Upvotes: 0

Piotrek332
Piotrek332

Reputation: 173

U can use lodash and function uniqBy from the library, it will be the fastest way :)

https://lodash.com/docs/4.17.15#uniqBy

Upvotes: 2

Adrita Sharma
Adrita Sharma

Reputation: 22213

You can create a generic function GetDistinctValues(Source, Filterkey) that will take any data array and any filter key of the objects in the array

Try like this:

  dataarray = [
    { alarma: 1797, nroSerie: "8764368", nombre: "Alarma cable cortado", fechaInicio: 1566194460, fechaFin: 1566311460 },
    { alarma: 1797, nroSerie: "8764368", nombre: "Alarma cable cortado", fechaInicio: 1566311460, fechaFin: 1566311580 },
    { alarma: 1797, nroSerie: "8764368", nombre: "Alarma cable cortado", fechaInicio: 1566311580, fechaFin: null }
  ]

  ngOnInit() {
    var distinctValue = this.GetDistinctValues(this.dataarray, 'alarma')
    console.log(distinctValue)
  }


  GetDistinctValues(Source: Array<any>, FilterKey: string = null): Array<any> {
    let DistinctArray = [];
    try {
      Source.forEach((e) => {
        if (FilterKey !== null && FilterKey !== undefined && FilterKey !== "") {
          if (DistinctArray.filter(((DE) => DE[FilterKey] === e[FilterKey])).length <= 0)
            DistinctArray.push(e);
        }
        else {
          if (DistinctArray.indexOf(e) === -1)
            DistinctArray.push(e);
        }
      });
    } catch (error) {
      DistinctArray = [];
    }
    return DistinctArray;
  }

Upvotes: 0

Related Questions