Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

121
Vistas
find out if object in two arrays of objects is unique

Compare two array of objects to find distinct values by all key values

So I found a lot of documentation like Get unique values by comparing old and new array objects but my case is a bit more complex.

In my case I want to compare two databases of prices, every week, finding out which prices have changed and then add only them to the existing database. I give you an example:

oldPrices = [{"date": "07-07-2022", "price": 129, "locationId": 1 }, {"date": "08-07-2022", "price": 139, "locationId": 1 }, {"date": "08-07-2022", "price": 139, "locationId": 2 }]
newPrices = [{"date": "07-07-2022", "price": 139, "locationId": 1 }, {"date": "08-07-2022", "price": 139, "locationId": 1 }, {"date": "08-07-2022", "price": 139, "locationId": 2 }]

Expected output to add to oldPrices:

[{"date": "07-07-2022", "price": 139, "locationId": 1 }]

What I have tried so far:

var data1 = {{(table17.data)}};
var data2 = {{ formatDataAsArray(table18.data) }};
      
const oldPrices = data1.map (({date, price, locationId}) => ({date, price, locationId}));
const newPrices = data2.map (({date, price, locationId}) => ({date, price, locationId}));

function getDifference(array1, array2) {
  return array1.filter(object1 => {
    return !array2.some(object2 => {
      return object1.price === object2.price;
    });
  });
}

const difference = [
  ...getDifference(oldPrices, newPrices),
  ...getDifference(newPrices, oldPrices)
];

console.log(difference)

Outcome = empty

There is only outcome, if the newPrice data contains prices, that were not included in the oldPrices data. This makes no sense, since I want to add all the objects containing combinations of the 3 keys, that are new (unique).

What I need is the function not only check the price of every object, but the combination of date, price and locationId, so that the output in this case would again be [{"date": "07-07-2022", "price": 139, "locationId": 1 }].

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

i am using js language

1- Use the filter() method to iterate over the first array. 2- Check if each object is not contained in the second array.

3- Repeat steps 1 and 2 for the second array. 4- Concatenate the results to get the complete difference.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I do not completely understand what the difference array should contain. If you wanted to compare the newPrices array to the oldPrices array and return only the object that has a different price compared to its corresponding object (one with the same date and locationId) then the following function will do the job:

const oldPrices = [{"date": "07-07-2022", "price": 129, "locationId": 1 }, {"date": "08-07-2022", "price": 139, "locationId": 1 }, {"date": "08-07-2022", "price": 139, "locationId": 2 }];
const newPrices = [{"date": "07-07-2022", "price": 139, "locationId": 1 }, {"date": "08-07-2022", "price": 139, "locationId": 1 }, {"date": "08-07-2022", "price": 139, "locationId": 2 }];

function getDifference(array1, array2) {
  return array1.filter(object1 => {
    return array2.find(object2 => {
      if (object1.date === object2.date && object1.locationId === object2.locationId && object1.price !== object2.price) {
        return object2
      }
    })
  });
}

const difference = [
  // ...getDifference(oldPrices, newPrices),
  ...getDifference(newPrices, oldPrices)
];

console.log(difference)

Keep in mind that if you run the getDifference function with oldPrices as the first argument and newPrices as the second argument the result will not be the same.

I hope this is what you are trying to achieve, if not please explain in more detail what the result should be and how would you like to achieve it.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I think this is good solution for your problem

const oldPrices = [
  { date: "07-07-2022", price: 129, locationId: 1 },
  { date: "08-07-2022", price: 139, locationId: 1 },
  { date: "08-07-2022", price: 139, locationId: 2 },
];
const newPrices = [
  { date: "07-07-2022", price: 139, locationId: 1 },
  { date: "08-07-2022", price: 139, locationId: 1 },
  { date: "08-07-2022", price: 139, locationId: 2 },
];

let old = oldPrices.map((p) => JSON.stringify(p));
let unique = newPrices.filter((p) => !old.includes(JSON.stringify(p)));

console.log(unique);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda