1

I am getting different results using JS filter and some methods when running within my node js app (server side) and in the browser console (client side)

I have the following 2 arrays of objects, and what I want to achieve is to create a new array of objects that does not include anything that is duplicated

export default async (req, res) => {
  const userAlerts = req.body;
  console.log(userAlerts);
  userAlerts = [ { 
    user_id: 232,
    match_id: 3918053,
    alert_type: 'OVER0.5FHCORNERS',
    alert_odds: '2.0',
    home_team: 'Pescara U19',
   },
   { user_id: 232,
     match_id: 3909005,
     alert_type: 'OVER0.5FHCORNERS',
     alert_odds: '2.0',
     home_team: 'Fortuna Koln U19',
   }
  ]

const existingAlerts = await queries.getUserTelegramAlerts(userID);
console.log(existingAlerts);
existingAlerts = [ { 
  user_id: 232,
  match_id: 3918053,
  alert_type: 'OVER0.5FHCORNERS',
  alert_odds: '2.0',
  home_team: 'Pescara U19',
}, 
{ 
  user_id: 232,
  match_id: 3909005,
  alert_type: 'OVER0.5FHCORNERS',
  alert_odds: '2.0',
  home_team: 'Fortuna Koln U19',
 }
]

const alertsToSet = userAlerts.filter(x => {
  return !existingAlerts.some(
    t =>
      t.user_id === x.user_id &&
      t.match_id === x.match_id &&
      t.alert_type === x.alert_type &&
      t.alert_odds === x.alert_odds
  );
});
console.log({ alertsToSet });
}

Logging out alertsToSet in the browser is empty, which is the desired behaviour, but when running this code server side, alertsToSet has 2 objects

Am i missing something or made a mistake ?

Richlewis
  • 15,070
  • 37
  • 122
  • 283
  • Does this answer your question? [How to remove all duplicates from an array of objects?](https://stackoverflow.com/questions/2218999/how-to-remove-all-duplicates-from-an-array-of-objects) – Kinglish Dec 03 '21 at 21:20
  • `const alertsToSet = [...new Set(existingAlerts, userAlerts)];` - and there are many other methods – Kinglish Dec 03 '21 at 21:21
  • @Kinglish I may be wrong, but I think, that if `existingAlerts` and `userAlerts` both already contain the same element, `alertsToSet` will also contain that element (just once, instead of twice), but from the code above the expectation seems to be that `alertsToSet` should be empty – derpirscher Dec 03 '21 at 21:25
  • 1
    @Richlewis: I just pasted your code into nodejs 16 and as expected, alertsToSet is an empty array. – derpirscher Dec 03 '21 at 21:26
  • @derpirscher I'm running `13.4.0` and its still not empty for me, would the version really make that much difference? – Richlewis Dec 03 '21 at 21:49
  • Probably not. `Array.filter` and `Array.some` are supported in nodejs from the very beginning and I doubt, there is an error in those methods ( eventhough you are using a very old development version of node) – derpirscher Dec 03 '21 at 21:52
  • @derpirscher the only other difference I can think of is that all the above logic is within an `async` method, though I cant think how that would make a difference? – Richlewis Dec 03 '21 at 21:53
  • Not for that exact part of the code. But of course if you are doing this inside some async method and try to access the result outside of it and are not correctly awaiting it there may be a difference. Have you tried pasting *that exact piece of code* into your node console? – derpirscher Dec 03 '21 at 21:56
  • @derpirscher Yes I just have, it's empty... fml haha.. ok so theres something else going on which i need to investigate – Richlewis Dec 03 '21 at 22:01
  • May it be that `userAlerts` and/or `existingAlerts` are read from an async data source? Any you don't correctly await at least one of them? – derpirscher Dec 03 '21 at 22:29
  • @derpirscher I've updated my question with the async function im using, both `userAlerts` and `existingAlerts` are available before the run of the filter/some methods – Richlewis Dec 03 '21 at 23:27
  • So the `req.body` and the result of `getUserTelegramAlerts` resp. are exactly the same as the hardcoded values? I doubt that. There has to be some sublte difference. Otherwise the filter would work ... – derpirscher Dec 04 '21 at 07:33
  • I was logging out an example I’ve been working with, so yes in this example they are exactly the same which should result to an empty array after the filter, anyway, thanks for taking the time to answer so far, I’ll continue investigating – Richlewis Dec 04 '21 at 08:48
  • You can also add additional logging to the callback of `some(...)` (ie log out the results of each an every comparison and check if that result meets your requirement. This will help you identifiy the source of the problem – derpirscher Dec 04 '21 at 09:11

0 Answers0