Skip to content

Instantly share code, notes, and snippets.

@shawn-sandy
Last active January 29, 2023 08:55
Show Gist options
  • Save shawn-sandy/6032a5dfc5463d89c5bfee6e51884716 to your computer and use it in GitHub Desktop.
Save shawn-sandy/6032a5dfc5463d89c5bfee6e51884716 to your computer and use it in GitHub Desktop.
map
"use strict";
const matter = [
null,
null,
null,
{
title: "Advanced title",
description: "Some advance story",
date: "2023-01-22T23:51:45.574Z",
},
{
title: "Another page",
description: "Page description",
},
null,
];
//filter array where matter is not null
const filteredMatter = matter.filter((item) => item !== null);
const pageMap = [
{
kind: "Meta",
data: {
index: "Introduction",
another: "Another Page",
advanced: "Advanced (A Folder)",
about: {
title: "About",
type: "page",
},
contact: {
title: "Contact ↗",
type: "page",
href: "https://twitter.com/shuding_",
newWindow: true,
},
},
},
{
kind: "MdxPage",
name: "about",
route: "/about",
},
{
kind: "Folder",
name: "advanced",
route: "/advanced",
children: [
{
kind: "MdxPage",
name: "satori",
route: "/advanced/satori",
},
],
},
{
kind: "MdxPage",
name: "advanced",
route: "/advanced",
frontMatter: {
title: "Advanced title",
description: "Some advance story",
date: "2023-01-22T23:51:45.574Z",
},
},
{
kind: "MdxPage",
name: "another",
route: "/another",
frontMatter: {
title: "Another page",
description: "Page description",
},
},
{
kind: "MdxPage",
name: "index",
route: "/",
frontMatter: {
title: "Introduction",
description:
"A simple, powerful and flexible site generation framework with everything you love from Next.js.",
},
},
];
// get from pageMap where kind is MdxPage
const mdxPages = pageMap.filter((item) => item.kind === "MdxPage");
// get from mdxPages where frontMatter is not null
const mdxPagesWithFrontMatter = mdxPages.filter(
(item) => item.frontMatter !== undefined
);
// get frontMatter from pageMap where kind is MdxPage
const mdxPagesFrontMatter = pageMap.filter(
(item) => item.kind === "MdxPage" && item.frontMatter !== undefined
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment