Dev Helpers
← Back

Build a tag-to-title directory

Build a tag-to-title directory. 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.flatMap((r) => r.tags))].map((tag) => [
    tag,
    rows.filter((r) => r.tags.includes(tag)).map((r) => r.title),
  ]),
);
console.log(JSON.stringify(result, null, 2));

Sample output

{
  "sea": [
    "Tide",
    "Reef"
  ],
  "blue": [
    "Tide"
  ],
  "sand": [
    "Dune"
  ],
  "gold": [
    "Dune",
    "Reef"
  ]
}