Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
Threading sets of relational data together

I'm calling an API where the response reduces repetition with a relational data structure where one set of data refers to a second set. Here is an example of the structure:

{
  products: [
    {
      name: 'Wine',
      restrictions: ['age'],
    },
    {
      name: 'Pain killers',
      restrictions: ['bulk-limit'],
    },
    {
      name: 'Chocolate',
      restrictions: [],
    },
  ],
  restrictions: [
    {
      type: 'age',
      minAge: 18,
    },
    {
      type: 'bulk-limit',
      maxQuantity: 2,
    },
  ],
}

The consumer needs to thread this data together by using the products that each contain a restrictions array of strings which relate to the data in the restrictions array. My first question is, does this data structure have a common name?

Secondly, I've written a function to thread the data together into a single array of products:

function mapRestrictionsToProducts(response: ProductsResponse): Product[] {
  let restrictionMap = new Map<string, Restriction>();
  response.restrictions.forEach((restriction) => {
    switch (restriction.type) {
      case 'age':
        return restrictionMap.set('age', {
          type: 'age',
          minAge: restriction.minAge,
        });

      case 'bulk-limit':
        return restrictionMap.set('bulk-limit', {
          type: 'BULK_BUY_LIMIT',
          maxQuantity: restriction.maxQuantity,
        });

      default:
        console.warn('Unknown restriction', restriction);
        return;
    }
  });

  return response.products.map(p => {
    let product: Product = {
      name: p.name,
      restrictions: p.restrictions.reduce<Restriction[]>(
        (prev, curr) => {
          let mappedRestriction = restrictionMap.get(curr);

          if (mappedRestriction) {
            prev.push(mappedRestriction);
          }

          return prev;
        },
        []
      ),
    };

    return product;
  });
}

Even though this function does what is needed, it does contain two array maps, one of which contains a reduce, and it feels overly complex. Are there any better options with which to iterate over this data and produce the same result?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda