Dev Helpers
← Back

Partition order lines by spend threshold

Partition order lines by spend threshold. Inspect the transformed data in the console.

Open this example in the interactive library

Source code

const rows = [
  { item: "tea", units: 2, cents: 350 },
  { item: "mug", units: 1, cents: 900 },
  { item: "tea", units: 3, cents: 325 },
  { item: "tin", units: 2, cents: 500 },
];
const result = Object.fromEntries(
  ["large", "small"].map((key) => [
    key,
    rows.filter((r) => (r.units * r.cents >= 900 ? "large" : "small") === key),
  ]),
);
console.log(JSON.stringify(result, null, 2));

Sample output

{
  "large": [
    {
      "item": "mug",
      "units": 1,
      "cents": 900
    },
    {
      "item": "tea",
      "units": 3,
      "cents": 325
    },
    {
      "item": "tin",
      "units": 2,
      "cents": 500
    }
  ],
  "small": [
    {
      "item": "tea",
      "units": 2,
      "cents": 350
    }
  ]
}