Home > front end >  How do I access a nested value in a one-to-many related field, within a multidimensional JavaScript
How do I access a nested value in a one-to-many related field, within a multidimensional JavaScript

Time:10-12

I have an Object called universe that looks like this:

Universes

[
    {
        "id": "cl94en0hs1x",
        "title": "My Universe",
        "published": false,
        "authorId": "cl94elyqvpl6bf",
        "author": {
            "name": "Jane"
        }
    },
    {
        "id": "cl94hlv5t028z",
        "title": "My other Universe",
        "published": false,
        "authorId": "cl94elyq",
        "author": {
            "name": "Jacob"
        }
    }
]

Each universe has a 1 to many relationship with another Object called Planet. (One universe can have many planets, but a planet can only belong to one universe.) This connection is seen by the field universeId in the Planet object, showing which universe the planet belongs to. See the planet object further below

When I am in an individual Universe, I can access the planets that the universe has, using include and select in Prisma like so:

export const getServerSideProps: GetServerSideProps = async (x: { params: { id: String} }) => {
    const universe = await prisma.universe.findUnique({
        where: {
            id: String(x.params?.id)
        },
        include: {
            planets: {
                select: {
                    title: true,
                   id: true,
                },
            },
            author: {
                select: { name: true, email: true },
            },
        },
    });
    return {
        props: universe,
    };
};

Then to get an individual planet object I use planets[0] which gives me the result below.

Planet

        {
            "title": "Earth",
            "id": "c4bd6e18c9564fde920cc0f85d4c0cbc"
        }

Problem

The problem I have is, I am trying to extract title and id from the planet object above so that I can save them as individual variables and use them. For example, I need the id field in order to navigate to the individual planet.

I can't seem to drill down any further after planets[0], so how do I drill down further to get id?

i tried planets[0][0] for title and planets[0][1] (this doesn't work, it's probably stupid, but i'm trying everything). Also tried planets[0].id, planets.id. None of them works.

I also tried looping over it, Object.Keys, Object.Entry. But I can not seem to get an individual value like c4bd6e18c9564fde920cc0f85d4c0cbc for the id. The closest I came to was:

id: c4bd6e18c9564fde920cc0f85d4c0cbc

How do I get the value only?

I want this:

c4bd6e18c9564fde920cc0f85d4c0cbc

CodePudding user response:

You use flat maps for your work efficiently. The flatMap() method returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level. (Ref: Mozilla)

CodePudding user response:

For anyone who might be interested, it ended up being a small thing I missed before. I would have deleted the question, but StackOverflow said I should leave it for fairness purposes, and in case someone needs it.

planets[0].id

This gives me the value i needed: c4bd6e18c9564fde920cc0f85d4c0cbc

  • Related