Home > Enterprise >  new Array Conditional values
new Array Conditional values

Time:11-10

I need to create a new array as you can see it has a key value that specifies the latitude and longitude.

I want the key values ​​that are equal to be set as 1 only but according to who has the highest count

[
    {
        "color":"green",
        "coment_calification":"Califica",
        "count":7,
        "key":"-13.0711552&-76.3723776&Califica",
        "latitud":"-13.0711552",
        "longitud":"-76.3723776"
    },
    {
        "color":"yellow",
        "coment_calification":"Reporte",
        "count":6,
        "key":"-13.0711552&-76.3723776&Reporte",
        "latitud":"-13.0711552",
        "longitud":"-76.3723776"
    },
    {
        "color":"green",
        "coment_calification":"Califica",
        "count":1,
        "key":"-13.1711552&-76.3423776&Califica",
        "latitud":"-13.1711552",
        "longitud":"-76.3423776"
    },
    {
        "color":"yellow",
        "coment_calification":"Reporte",
        "count":2,
        "key":"-13.1711552&-76.3423776&Reporte",
        "latitud":"-13.1711552",
        "longitud":"-76.3423776"
    }
]
 let result = count.filter((e) => e && e.count && e.key == e.key);

 let datas = result;

CodePudding user response:

Is this what you're looking for?

const result = arr.reduce((acc, cur) => {
  const matchIndex = acc.findIndex(saved => 
      saved.latitud === cur.latitud
      && saved.longitud === cur.longitud
  );

  if (matchIndex !== -1) {
    if (cur.count > acc[matchIndex].count) {
      acc.splice(matchIndex, 1, cur);
    }
    return acc;
  }


  return [...acc, cur];
}, []);

CodePudding user response:

It looks like the array contains objects that differ mostly by their count props but have several other duplicate props. I think I understand the OP to want to remove these almost-duplicates retaining the one with the highest count.

One way to do this is to sort the array to be descending by count, walk through them, pushing onto a result only those that aren't already in the result.

The only thing not super clear in the OP is what constitutes sameness besides the count field. Here, we guess that the coment_calification key being the same means the objects are to be treated as duplicate.

const objects = [
    {
        "color":"green",
        "coment_calification":"Califica",
        "count":7,
        "key":"-13.0711552&-76.3723776&Califica",
        "latitud":"-13.0711552",
        "longitud":"-76.3723776"
    },
    {
        "color":"yellow",
        "coment_calification":"Reporte",
        "count":6,
        "key":"-13.0711552&-76.3723776&Reporte",
        "latitud":"-13.0711552",
        "longitud":"-76.3723776"
    },
    {
        "color":"green",
        "coment_calification":"Califica",
        "count":1,
        "key":"-13.1711552&-76.3423776&Califica",
        "latitud":"-13.1711552",
        "longitud":"-76.3423776"
    },
    {
        "color":"yellow",
        "coment_calification":"Reporte",
        "count":2,
        "key":"-13.1711552&-76.3423776&Reporte",
        "latitud":"-13.1711552",
        "longitud":"-76.3423776"
    }
];

objects.sort((a,b) => b.count-a.count) // sort descending
const result = [];

for (let object of objects) {
  // supposing matching coment_calification props means a match
  let alreadyInResult = result.find(r => r.coment_calification === object.coment_calification)
  if (!alreadyInResult) result.push(object);
}
console.log(result)

  • Related