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}
4
  • array.filter() Commented Aug 28, 2019 at 8:43
  • array.filter will return all elements with specified condition, it's opposite. You can use Map, 'alarma' value as key and whole element as value Commented Aug 28, 2019 at 8:46
  • Possible duplicate of JavaScript: Remove duplicates of objects sharing same property value Commented Aug 28, 2019 at 8:49
  • I try something like this but isn´t exactly that i need it: array.filter((el, i, a) => i === a.indexOf(el)); Commented Aug 28, 2019 at 8:57

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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;
  }

Comments

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

Comments

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}]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.