Dev Helpers
← Back

Count entries by number of tags

Count entries by number of tags. Inspect the transformed data in the console.

Open this example in the interactive library

Source code

const rows = [
  { title: "Tide", tags: ["sea", "blue"] },
  { title: "Dune", tags: ["sand", "gold"] },
  { title: "Reef", tags: ["sea", "gold"] },
  { title: "Blank", tags: [] },
];
const result = Object.fromEntries(
  [...new Set(rows.map((r) => r.tags.length))].map((n) => [
    n,
    rows.filter((r) => r.tags.length === n).length,
  ]),
);
console.log(JSON.stringify(result, null, 2));

Sample output

{
  "0": 1,
  "2": 3
}