Dev Helpers
← Back

Count entries by number of tags

Count entries by number of tags. Expand the input records to compare them with the transformed collection.

Open this example in the interactive library

Live preview

Source code

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <title>Count entries by number of tags</title>
  <style>
    * { box-sizing: border-box; }
    body { margin: 0; padding: clamp(12px,4vw,44px); background: #f6f2ea; color: #252927; font: 16px/1.5 system-ui,sans-serif; }
    main { max-width: 68rem; margin: auto; min-width: 0; overflow-wrap: anywhere; }
    fieldset,label,.stack,.box,input { min-width:0; }
    h1,h2,h3,p,figure,dl { margin: 0; }
    h1 { font: 500 clamp(2rem,6vw,4.75rem)/1.02 Georgia,serif; letter-spacing:-.045em; }
    h2 { font: 500 clamp(1.4rem,3vw,2.4rem)/1.12 Georgia,serif; }
    h3 { font-size: 1.05rem; }
    a { color: #713b24; text-underline-offset: .18em; }
    a,button,summary,label { touch-action: manipulation; }
    button, input, select, textarea { font: inherit; max-width:100%; }
    button,.action { display:inline-block; padding:.55rem .85rem; background:#27453f; color:white; border:0; border-radius:.3rem; cursor:pointer; text-decoration:none; }
    input:not([type=checkbox],[type=radio],[type=range],[type=color]),select,textarea { border:1px solid #878d82; border-radius:.25rem; padding:.55rem; background:white; color:#202822; }
    :focus-visible { outline:3px solid #a34720; outline-offset:3px; }
    details { padding:.7rem 0; border-bottom:1px solid #bcbcaf; }
    summary { cursor:pointer; font-weight:650; }
    dt,legend { font-weight:700; }
    dd { margin:0; }
    .stack,.grid,.split { display:grid; gap:1rem; }
    .row { display:flex; gap:.75rem; flex-wrap:wrap; align-items:center; }
    .grid { grid-template-columns:repeat(auto-fit,minmax(min(100%,12rem),1fr)); }
    .box { border:1px solid #c9c8bc; background:#fffdf8; padding:clamp(.85rem,2vw,1.4rem); }
    .ink { background:#243d37; color:#fffaf1; padding:clamp(1rem,4vw,3rem); }
    .ink a { color:#fff4de; }
    .kicker { color:#805033; font:700 .78rem/1.5 ui-monospace,monospace; letter-spacing:.08em; text-transform:uppercase; }
    .ink .kicker { color:#efbd98; }
    .pill { display:inline-block; border:1px solid currentColor; border-radius:2rem; padding:.15rem .65rem; font-size:.84rem; }
    pre { overflow:auto; max-width:100%; padding:1rem; background:#213831; color:white; }
    table { border-collapse:collapse; width:100%; }
    th,td { padding:.55rem; border-bottom:1px solid #bcbcaf; text-align:left; }
    progress,meter { max-width:100%; }
    :where(.split,.grid)>* { min-width:0; }
    @media(min-width:44rem) { .split { grid-template-columns:repeat(2,minmax(0,1fr)); align-items:start; } }
    @media(prefers-reduced-motion:reduce) { * { scroll-behavior:auto !important; } }
  </style>
  <style>
  dl {
    display: grid;
    gap: 0.6rem;
    margin: 0;
  }
  dl > div {
    display: grid;
    gap: 0.35rem;
    padding: 0.6rem;
    border-bottom: 1px solid #bbb;
  }
  dt {
    font-weight: 700;
  }
  dd {
    margin: 0;
    overflow-wrap: anywhere;
  }
  @media (min-width: 32rem) {
    dl > div {
      grid-template-columns: minmax(5rem, 1fr) 3fr;
    }
  }
</style>
</head>
<body><main><h2>Count entries by number of tags</h2>
<details>
  <summary>Input records</summary>
  <pre>
[
  {
    "title": "Tide",
    "tags": [
      "sea",
      "blue"
    ]
  },
  {
    "title": "Dune",
    "tags": [
      "sand",
      "gold"
    ]
  },
  {
    "title": "Reef",
    "tags": [
      "sea",
      "gold"
    ]
  },
  {
    "title": "Blank",
    "tags": []
  }
]</pre>
</details>
<dl class="" id="result"></dl>
<script>
  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,
    ]),
  );
  const list = document.querySelector("#result");
  list.replaceChildren();
  for (const [key, value] of Object.entries(
    typeof result === "object" && result !== null ? result : { value: result },
  )) {
    const row = document.createElement("div");
    const label = document.createElement("dt");
    label.textContent = key;
    const detail = document.createElement("dd");
    detail.textContent =
      typeof value === "string" ? value : JSON.stringify(value);
    row.append(label, detail);
    list.append(row);
  }
  if (!list.children.length) list.after("No matching entries.");
</script>
</main></body>
</html>