1

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

3 Answers3

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.

  • 1
    Hello Syed, thank you for the quick reply. Since I am new to writing JS I don´t know how to change my code/which functions to use. Could you provide me with the adapted code snipped of mine? – leonleprofessionnel Aug 07 '22 at 09:01
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 10 '22 at 07:12
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.

tadej
  • 86
  • 7
  • Hey tadej, I had the same idea, using an if statement with the included conditions. Didn´t use it with the .find function though, so it didn´t work. Your code works like a charm, thank you for sharing! – leonleprofessionnel Aug 08 '22 at 09:09
  • I am glad my soultion worked. Please consider marking my answer as the accepted answer, so that others with the same problem can quickly find the solution. Happy coding :) – tadej Aug 08 '22 at 12:20
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);