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

93
Visualizações
Combine array of keys and array of values into object

I have an array with labels

columns = ["col1", "col2", "col3", "col4"]

and thousands of arrays with values

values = [ 
    ['value1','value2','value3','value4'],
    ['value1','value2','value3','value4'],
    ['value1','value2','value3','value4'],
    // ...
]

I want to iterate the values and create a single array with this structure:

result = [ 
    { col1: "val1", col2: "val2", col3: "val3", col3: "val4" },
    { col1: "val1", col2: "val2", col3: "val3", col3: "val4" },
    { col1: "val1", col2: "val2", col3: "val3", col3: "val4" },
]

I tried to iterate both and return the object dynamically, but without success:

const columns = ["col1", "col2", "col3", "col4"];
const values = [ 
    ['value1','value2','value3','value4'],
    ['value1','value2','value3','value4'],
    ['value1','value2','value3','value4'],
];

const lastArr = values.map((e) =>
    columns.map((col) => {
        return { col: e[0] };
    })
);

console.log(lastArr);

// or manually 

const raw = values.map(e => 
    columns.map(col => {
        return {
            col1: e[0],
            col2: e[1],
            col3: e[2],
            col4: e[3],
        }
    })
);

console.log(raw);

Neither attempts return the desired result.

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

You can use Object.fromEntries, like so:

const columns = ["col1", "col2", "col3"];
const values = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
    [10, 11, 12]
];

const result = values.map(row =>
    Object.fromEntries(columns.map((col, i) =>
        [col, row[i]]
    ))
)

console.log(result);

about 4 years ago · Santiago Gelvez Relatório

0

It would be more understandable if your examples were more consistent and the values were a bit more distinguishable but this should do the trick

values.map(v => Object.fromEntries(columns.map((c,i) => [c, v[i]])));
about 4 years ago · Santiago Gelvez Relatório

0

Combination of map and reduce can help here.

const lastArr = this.rawData.map((row) => {
  const colsMap =  cols.map((col, index) => {
    return { [col]: row[index] };
  });
  return colsMap.reduce((acc, curr) => { ...acc, curr }, {});
});
about 4 years ago · Santiago Gelvez 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