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

218
Visualizações
How to combine two arrays into one object so that the values of the first array are keys, and the values of the second array are values

Two objects are given const en = ["mon",  "tue",  "wed",  "thu",  "fri",  "sat",  "sun"]; const de = ["montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"];

It is necessary to combine these 2 arrays into one object so that the values of the first array are keys, and the values of the second array are values. How to do it?

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

0

You can do it using Object.fromEntries.

const 
  en = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"],
  de = ["montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"],
  result = Object.fromEntries(en.map((k, i) => [k, de[i]]));

console.log(result);

You can also reduce one of the arrays into the desired object.

const 
  en = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"],
  de = ["montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"],
  result = en.reduce((acc, k, i) => ({ ...acc, [k]: de[i] }), {});

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

Another solution using map, easy to understand:

const en = ["mon",  "tue",  "wed",  "thu",  "fri",  "sat",  "sun"];
const de = ["montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"];
let result = {};

en.map((item, index) => result[item] = de[index])
about 4 years ago · Juan Pablo Isaza Relatório

0

Iterate over over the first array and collect the elements of the second at th e matching index:

   const en = ["mon",  "tue",  "wed",  "thu",  "fri",  "sat",  "sun"];
   const de = ["montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"];
   let dict={}
 ;

   en.forEach ( (px, pn) => { dict[px] = de[pn]; });

   console.log(dict);

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