skip to content
A cartoon cactus looking at the 'Astro.build' logo Vinayak Sarawagi

Improving Lookups in your code

/ 3 min read

Problem Statement

Many a times, we get an array of objects X and an array of elements Y, now we will need to lookup elements of X for each value present in Y.

Let’s say you have X similar to below, a list of books with their respective authors.

x.js
const X = [
{
id: "b001",
title: "Pride and Prejudice",
author: {
id: "a001",
name: "Jane Austen",
},
publishedYear: 1813,
genre: "Romance",
},
{
id: "b002",
title: "1984",
author: {
id: "a002",
name: "George Orwell",
},
publishedYear: 1949,
genre: "Dystopian Fiction",
},
{
id: "b003",
title: "One Hundred Years of Solitude",
author: {
id: "a003",
name: "Gabriel García Márquez",
},
publishedYear: 1967,
genre: "Magical Realism",
},
{
id: "b004",
title: "The Great Gatsby",
author: {
id: "a004",
name: "F. Scott Fitzgerald",
},
publishedYear: 1925,
genre: "Literary Fiction",
},
{
id: "b005",
title: "Brave New World",
author: {
id: "a002",
name: "George Orwell",
},
publishedYear: 1932,
genre: "Dystopian Fiction",
}
];

And Y as a list of books which we need to return to the client.

y.js
const Y = [
'Brave New World',
'The Great Gatsby'
];

Now that we have X and Y both, we need to return only the details of the books present in the Y, which means our client shall receive,

[
{
id: "b004",
title: "The Great Gatsby",
...
},
{
id: "b005",
title: "Brave New World",
...
}
]

Solution

Now, to solve this, our first instinct in JS would be to use the Array.filter method and do a quick iteration for every element of Y over X.

const filteredBooks = X.filter((book) => {
return Y.includes(book.name);
})

Solution

Now, to quickly solv

What are admonitions

Admonitions (also known as “asides”) are useful for providing supportive and/or supplementary information related to your content.

How to use them

To use admonitions in Astro Cactus, wrap your Markdown content in a pair of triple colons :::. The first pair should also include the type of admonition you want to use.

For example, with the following Markdown:

:::note
Highlights information that users should take into account, even when skimming.
:::

Outputs:

Admonition Types

The following admonitions are currently supported:

  • note
  • tip
  • important
  • warning
  • caution

Note

:::note
Highlights information that users should take into account, even when skimming.
:::

Tip

:::tip
Optional information to help a user be more successful.
:::

Important

:::important
Crucial information necessary for users to succeed.
:::

Warning

:::warning
Critical content demanding immediate user attention due to potential risks.
:::

Caution

:::caution
Negative potential consequences of an action.
:::

Customising the admonition title

You can customise the admonition title using the following markup:

:::note[My custom title]
This is a note with a custom title.
:::

Outputs: