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

195
Visualizações
Combine two one dimensional arrays to create a cartesian table

I hope you are good.

I am struggling to create a compatible data type in javascript to display a cartesian like table where we have a vertical and a horizontal header. Basically I have 3 one dimensional arrays where the first two are the table headers, and the third has the combination of those two by id's (basically the table cells).

let horizontal_header = [
      { id: 1, name: 'h1' },
      { id: 2, name: 'h2' },
    ];
let vertical_header = [
      { id: 10, name: 'r1' },
      { id: 11, name: 'r2' },
    ];
let cells = [
      { hid: 1, vid: 10, id: 7, name: 'c1' },
      { hid: 1, vid: 11, id: 8, name: 'c2' },
      { hid: 2, vid: 10, id: 9, name: 'c3' },
      { hid: 2, vid: 11, id: 10, name: 'c4' },
    ],

Also it can happen that a combination might not exists in that case, I want to enter an empty cell or something obvious that this cell is missing.

I want to create a table like below:

h1 h2
r1 c1 c3
r2 c2 c4

I would appreciate any suggestion and be very thankful to help me solve this complex use-case using Angular for rendering the table template.

Thank you.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I'd approach this problem by parsing the cells into more table-render friendly format like below. Note: I used ### separator, you can use anything that suits for coding practice.

let output = {};

cells.forEach(cell => {
  output[cell.hid + '###' + cell.vid] = {
    id: cell.id,
    name: cell.name,
  };
});

After that, you can use the output object to render the table cell as you already know the combination of hid and vid. You can prepare/render your table rows as below.

const rows = [];
for (let i = 0; i < horizontal_header.length; i++) {
  const row = [];
  for (let j = 0; j < vertical_header.length; j++) {
    const hid = horizontal_header[i];
    const vid = vertical_header[j];
    if (output[hid + '###' + vid]) {
      row.push(output[hid + '###' + vid].name);
    } else {
      row.push('-');
    }
  }
  rows.push(row);
}
about 4 years ago · Juan Pablo Isaza Relatório
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