2

I was trying to remove ids from a new request that have already been requested. In the code below I was expecting for the ...56 id to be removed and only the ...81 id to remain. 56 was removed but to my surprise 81 had been decreased to 80.

const oldIds = [
  10032789416531456
];

const newIds = [
  10032789435529381,
  10032789416531456
];

const result = newIds.filter(newId => !oldIds.some(oldId => oldId === newId));
console.log(result)
//Expected result is: [10032789435529381], instead I get [10032789435529380]

I was able to solve this problem by using strings for ids instead of numbers.

Alex Malcolm
  • 529
  • 1
  • 7
  • 18
  • 4
    You're exceeding the `Number.MAX_SAFE_INTEGER` value with those values. They cannot be represented exactly in JavaScript numbers. Using strings is probably the simplest solution. – Pointy Oct 29 '18 at 19:30

1 Answers1

3

If you type 10032789435529381 in the js console, it returns 10032789435529380. You've exceeded the capacity of the javascript integer, causing it to be treated as a less precise floating point number.

This is part of the reason why using strings for IDs instead of numbers is typically recommended.

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337