skip to Main Content

I have a json file that contains individual events and their markets like so:

   {
  "eventId": "317a66b5-3da3-4ab3-9273-434441d87f20",
  "markets": [
    [
      {
        "id": "38193a94-036b-41a2-a874-fe63ca8aaff4"
      },
      {
        "id": "1790671e-81c5-4ff4-885a-1f8eee0b0e6b"
      },
      {
        "id": "e37551d3-9e95-4701-8515-df88f4bb2180"
      },
      {
        "id": "03d8fea1-90f9-4b76-84dd-44e5ed072bd0"
      },
      {
        "id": "91ef3269-0c88-428f-819a-c0fd56a630e3"
      },
      {
        "id": "fdfc525a-a4ea-4956-b81b-ede2d8722a0b"
      },
      {
        "id": "e9d978e8-45d4-421c-b75e-57bd1907fa9f"
      },
      {
        "id": "ab344bbc-1f8e-4758-b86d-7615affe1295"
      },
      {
        "id": "00e32dc7-73c8-44dd-8f7f-f988d7f42992"
      }
    ]
  ]
},
{
  "eventId": "93a9e201-2d33-4358-89e6-f7d140b09a2e",
  "markets": [
    [
      {
        "id": "aac79642-f11d-4f4b-8048-6ac96843ccf1"
      },
      {
        "id": "8981b626-7357-4e6f-a998-d1e36515261a"
      },
      {
        "id": "045c235a-1fb8-4a9f-b2c5-13da379b6ad8"
      },
      {
        "id": "786c86ad-f465-4f6c-b5a3-4114a54a05b6"
      },
      {
        "id": "86618747-94df-43e4-a8a4-8613419f7217"
      },
      {
        "id": "845b9235-242f-4e0c-827e-4cdfce6ed71d"
      },
      {
        "id": "20ad2669-73e2-4d56-a9b6-8834efa8d95a"
      },
      {
        "id": "5805d0ba-f407-48e7-be52-af2892ccb355"
      },
      {
        "id": "66065767-c46c-4656-9ff9-c81f30704896"
      }
    ]
  ]
},
...

I have included a piece of code where I want to randomly select an event and output it’s event id and all its markets:

 const randomIndex = Math.floor(
        Math.random() * data.length
      );
    let eventId = data[randomIndex].eventId;
    let marketIds = data[randomIndex].markets;

However, I notice for the marketIds variable, it outputs each market id like this:

[
      {
        "id": "38193a94-036b-41a2-a874-fe63ca8aaff4"
      },
      {
        "id": "1790671e-81c5-4ff4-885a-1f8eee0b0e6b"
      },
      {
        "id": "e37551d3-9e95-4701-8515-df88f4bb2180"
      },
      {
        "id": "03d8fea1-90f9-4b76-84dd-44e5ed072bd0"
      },
      {
        "id": "91ef3269-0c88-428f-819a-c0fd56a630e3"
      },
      {
        "id": "fdfc525a-a4ea-4956-b81b-ede2d8722a0b"
      },
      {
        "id": "e9d978e8-45d4-421c-b75e-57bd1907fa9f"
      },
      {
        "id": "ab344bbc-1f8e-4758-b86d-7615affe1295"
      },
      {
        "id": "00e32dc7-73c8-44dd-8f7f-f988d7f42992"
      }
    ]

I was hoping the marketIds will be returned like this (without the curly braces):

[
        "38193a94-036b-41a2-a874-fe63ca8aaff4",
        "1790671e-81c5-4ff4-885a-1f8eee0b0e6b",
        "e37551d3-9e95-4701-8515-df88f4bb2180",
        "03d8fea1-90f9-4b76-84dd-44e5ed072bd0",
        "91ef3269-0c88-428f-819a-c0fd56a630e3",
        "fdfc525a-a4ea-4956-b81b-ede2d8722a0b",
        "e9d978e8-45d4-421c-b75e-57bd1907fa9f",
        "ab344bbc-1f8e-4758-b86d-7615affe1295",
        "00e32dc7-73c8-44dd-8f7f-f988d7f42992"
    ]

How can I get the data to be returned as a list of values?

2

Answers


  1. You’re close, you just need to use .map() to extract the IDs and return a new array.

    const randomIndex = Math.floor(Math.random() * data.length);
    let eventId = data[randomIndex].eventId;
    let marketIds = data[randomIndex].markets[0].map(market => market.id);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search