0

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}
El Hombre Sin Nombre
  • 2,906
  • 18
  • 49
  • 92

4 Answers4

2

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

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

Piotrek332
  • 163
  • 2
  • 13
0

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;
  }
Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
0

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

0

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}]
Yevhenii Dovhaniuk
  • 1,073
  • 5
  • 11