Dev Helpers
← Back

Compare pack unit prices

Compare pack unit prices using JavaScript.

Open this example in the interactive library

Source code

const packs = [
  { n: 6, cents: 600 },
  { n: 10, cents: 950 },
];
console.log(packs.toSorted((a, b) => a.cents / a.n - b.cents / b.n)[0]);

Sample output

{ n: 10, cents: 950 }