JSFiddle - React, Tailwind, and code Playground

by Renan Ribeiro Brando

JavaScript

const journeys = [
      {
        origin: 'Blackfriars',
        destination: 'Manchester',
        links: [
          {
            rel: 'alternative-journeys',
            href:
              '/api/orders/127235728927/products/84e64c23-e505-4d05-9679-dcc11b7556dd/alternative-journeys/outward',
            meta: {
              method: 'GET',
            },
          },
        ],
      },
      {
        origin: 'Manchester',
        destination: 'Blackfriars',
        links: [
          {
            rel: 'alternative-journeys',
            href:
              '/api/orders/127235728927/products/84e64c23-9679-4d05-e505-dcc11b7556dd/alternative-journeys/outward',
            meta: {
              method: 'GET',
            },
          },
        ],
      },
];

const getAlternativeJourneyLinks = (journeys) =>
  journeys
    .filter((journey) => journey.links !== undefined)
    .flatMap((journey) =>
      journey.links
        .filter((link) => (link !== undefined && link.rel !== undefined && link.rel.toLowerCase() === 'alternative-journeys'))
        .map((link) => link.href)
    );

console.log(getAlternativeJourneyLinks(journeys))